PHP SQL JOIN避免ID重复
问题描述:
I am using this code:
SELECT * FROM Table1
JOIN Table2 USING(venue_id)
WHERE table2.location = '$MyVariable'
Then to count the number of records return:
$num_rows = mysql_num_rows($result);
echo "$num_rows";
It works great but venue_id in Table1 has lots of entries and I only want it to get one per venue_id
How can I make it so it only returns 1 venue_id instance?
我正在使用此代码: p>
SELECT * FROM Table1
JOIN Table2 USING(venue_id)
WHERE table2.location ='$ MyVariable'
code> pre>
然后计算返回的记录数: p> \ n
$ num_rows = mysql_num_rows($ result);
echo“$ num_rows”;
code> pre>
它工作得很好但是Table1中的venue_id有 很多条目,我只希望它每个venue_id得到一个 p>
如何才能使它只返回1个venue_id实例? p>
div>
答
Use GROUP BY
clause,
SELECT * FROM Table1
JOIN Table2 USING(venue_id)
WHERE table2.location = '$MyVariable'
GROUP BY `Table1`.`venue_id`