table com
id | com | id_status
1 testing 1 1
2 testing 2 1
3 testing 3 2
4 testing 4 2
table update
id_status | update
1 update 1
2 update 2
3 update 3
SELECT `update`.`update`, `com`.`com` as comen
FROM `update`
LEFT JOIN `com` ON `update`.`id_status`=`com`.`id_status`
it results :
update 1
testing 1
update 1
testing 2
update 2
testing 3
update 2
testing 4
the update table results duplicate
i've done using group by update
.id_status
, it results :
update 1
testing 1
testing 2
update 3
the table com only resut 1 row
EDIT--
i got syntax from this MySQL LEFT JOIN display duplicate rows
$first = true;
while($row = $query->fetch_object()){
if($first){
echo $row->update;
$first = false;
echo "<br>";
}
echo $row->comen;
echo "<br>";
}
update 1
testing 1
testing 2
testing 3
testing 4
it fetch only 1 table row
i want the results looks like this :
update 1
testing 1
testing 2
update 2
testing 3
testing 4
update 3
...
--
how the right syntax work or maybe the query?
Try This, I think this will resolve:-
SELECT `update`.`update`, group_concat(`com`.`com`) as comen
FROM `update`
LEFT JOIN `com` ON `update`.`id_status`=`com`.`id_status`
Group by `update`.`update`
and use your php as like:-
while($row = $query->fetch_object()){
echo $row->update;
$comen = explode(",", $row->comen);
foreach($comen as $values){
echo "<br>";
echo $values;
}
echo "<br>";
}
Use INNER JOIN instead of left join
SELECT `update`.`update`, `com`.`com` as comen
FROM `update` INNER JOIN `com` ON `update`.`id_status`=`com`.`id_status`;
or can use DISTINCT keyword to avoid duplicate results
SELECT DISTINCT `com`.`id_status`, `update`.`update`, `com`.`com` as comen
FROM `update` LEFT JOIN `com` ON `update`.`id_status`=`com`.`id_status`;
According to your query , the result should have ok. Let have a look that the table com contain two value those id_status=1.
Now if you leftjoin com table with update table then the query will give 2 row corresponding id_status=1 because com table contain two row which id_status is 1.
if you want to get only any one row accroding to id_status then you have to use group by update.id-status or group by com.id-status
SELECT `up`.`up`, `com`.`com` as comen FROM `up` LEFT JOIN `com` ON `up`.`id_status`=`com`.`id_status` group by up.id_status
I used up instead of update because update is a keyword. Table:
Result: