我在名为advertisements的表格中有这些值:
id sitecatid locationid
1 12 3
2 12 6
3 12 8
4 12 8
5 12 8
6 10 18
7 13 20
8 14 21
我想这样返回结果:
sitecatid locationid
12 3
10 6
13 8
14 18
20
21
基本上我只想要具有唯一sitecatid的任何记录的第一个实例 和locationid。
更新
我尝试了这个查询但结果不正确:
SELECT DISTINCT sitecatid, locationid
FROM advertisements
WHERE MATCH (sitetitle,sitedescription)
AGAINST ('$q' IN BOOLEAN MODE) AND status='approved'
以上查询的结果:
sitecatid locationid
12 3
12 6
12 8
10 18
14 20
13 21
更新
我可以使用两个sitecatid
的查询和locationid
的下一个查询:
SELECT DISTINCT sitecatid
FROM advertisements
WHERE MATCH (sitetitle,sitedescription)
AGAINST ('$q' IN BOOLEAN MODE) AND status='approved'
AND
SELECT DISTINCT locationid
FROM advertisements
WHERE MATCH (sitetitle,sitedescription)
AGAINST ('$q' IN BOOLEAN MODE) AND status='approved'
但我想使用一个查询对于这两个
答案 0 :(得分:0)
我认为实现这一目标的唯一方法是在一列而不是两列中检索数据,因为您试图从一行中的不同记录中获取值,我认为这是不可能的。
SELECT 'SITECATID' UNION
SELECT DISTINCT sitecatid
FROM advertisements
WHERE MATCH (sitetitle,sitedescription)
AGAINST ('$q' IN BOOLEAN MODE) AND status='approved' UNION
SELECT 'LOCATIONID' UNION
SELECT DISTINCT locationid
FROM advertisements
WHERE MATCH (sitetitle,sitedescription)
AGAINST ('$q' IN BOOLEAN MODE) AND status='approved';