2016-12-01 48 views
-1

一个单线我有这样的MySQL查询:从MySQL选择时仅本

SELECT a.full_address 
FROM games_server AS a 
JOIN orders_order AS b 
ON b.server_id = a.id 
WHERE service_type = 'color' 
AND status = 'running' ORDER BY RAND()" 

它完美细,并选择仅当SERVICE_TYPE是“颜色”的值。 但我需要的是,只有当只有一行service_type = color且没有其他行具有不同的service_type值时,才会选择'full_address'。因为对于一个full_address = a,可能有多行。例如。 full_address = a,service_type = color,同一个full_address = a,service_type = top。在那个特殊情况下,当它们中有两个时 - 它不能被选中。只有当有一行service_type = color时才有。

我希望任何人都能理解我说的话。对不起,如果我不能更清楚。

+0

如果你发布一些样本数据和预期的输出,理解你的问题会更有帮助。 – Susang

回答

1

试试这个(假设games_server有ID):

SELECT a.full_address FROM games_server AS a 
JOIN orders_order AS b ON b.server_id = a.id 
WHERE status = 'running' 
GROUP BY a.id 
HAVING group_concat(distinct service_type) = 'color' 
ORDER BY RAND() 
+0

谢谢你的建议!但不幸的是它返回空白。 –

+0

yupp,对不起,忘了添加GROUP BY,希望你对games_server的主键是“id” – 2oppin

+0

像魅力一样工作。你是一个拯救生命的人!非常感谢你。 :) –

0

如果我理解正确你的问题,子查询应该是有用的在这里。

SELECT a.full_address 
FROM games_server AS a 
JOIN orders_order AS b 
ON b.server_id = a.id 
WHERE service_type in 
     (select service_type 
     from orders_order 
     group by service_type 
     having COUNT(service_type)=1) 
AND status = 'running' ORDER BY RAND() 
+0

2oppin已经提供了答案,但谢谢你的任何方式。 –