PHP, MYSQL - joining 2 tables where 1 table has multiple entries of primary key

I got 2 tables which I want to select via mysql_query in php.

  • table1

columns: specialID, Name, Information, Start, created, byUser, ziparea

  • table2

columns: ID, ziparea, cusID, title, contact, mail

$result = mysql_query("SELECT * FROM table1 JOIN table2 ON table1.ziparea = table2.ziparea WHERE table1.specialID='".$_REQUEST["specialID"]."'");
while($var = mysql_fetch_array($result)) {
}

table1 has exactly 1 entry for each ziparea from 01 to 99. table2 has multiple entries for zipareas.

The problem which seems to occur is that the result seems to be something like:

  • table1-data --- data1 ---- data1 --- data1
  • ziparea -------- 24 -------- 24 -------- 24
  • table2-data --- data1 ---- data2 --- data3

means the data of table1 gets copied. I want the cells to be empty, because if I i.e. ask for table1.Name in php,

<?php echo utf8_encode($var["Name"]); ?>

it should only give 1 data... now it comes with 3 copies of that one data I need.

How can I solve this problem?

Answers


Try adding in a GROUP BY into your SQL Query, such:

$result = mysql_query("SELECT table1.Name FROM table1 JOIN table2 ON table1.ziparea = table2.ziparea WHERE table1.specialID='".$_REQUEST["specialID"]."' GROUP BY table1.Name");

Need Your Help

How to add data parameter in ajaxSend properly?

javascript php jquery ajax jquery-1.4

In some circumstance I need to add POST parameter programmatically to AJAX request.

Should a class have the same name as the namespace?

.net naming-conventions namespaces class-design

I'm designing a namespace to hold a set of classes that will handle user related tasks for a several different applications. (Log-in, authenticate etc)