[英]How can I select mysql data with a union statement where the data matches each select statement, i.e. combine the AND operator with UNION?
Lets say I have a table with just two columns: name and mood . 可以说我有一个只有两列的表格: name和mood 。 A row holds a persons name, and their mood, if they have multiple moods, then multiple rows are stored in the DB. 一行包含一个人的名字,以及他们的心情,如果他们有多种心情,则多行存储在数据库中。
For example, in the database is John, who is happy, excited, and proud. 例如,数据库中是快乐,兴奋和自豪的约翰。
This is represented as 这表示为
John Happy
John Excited
John Proud
What I want to do is select the name based on several moods being met. 我要做的是根据遇到的几种心情选择名称。 Similiar to the UNION: 与联盟类似:
SELECT name WHERE mood=Happy
UNION
SELECT name WHERE mood=Excited
UNION
SELECT name WHERE mood=Proud
However using the above union would result in: 但是,使用上述联合将导致:
John
John
John
Union all would result in one single result of John, but it would also select any names that only match one of the queries. 统一所有将产生John的单个结果,但也会选择仅与查询之一匹配的任何名称。
I can think of a few algorithms which would take each individual mysql_result resource (I'm using PHP), and look for matching results, but what I want to know is whether MySQL already facilitates this. 我可以想到一些算法,这些算法将占用每个单独的mysql_result资源(我正在使用PHP),并寻找匹配的结果,但是我想知道的是MySQL是否已经在促进这一工作。
I realise the above is quite a vague generalisation, needless to say my actual program is alot more complicated and I've dumbed it down a little for this question, please don't hesitate to ask questions. 我意识到以上内容是一个模糊的概括,不用说我的实际程序要复杂得多,对于这个问题,我已经作了一些说明,请随时提出问题。
Provided you have no duplicates, you can do it with a subquery: 如果没有重复项,则可以使用子查询来完成:
SELECT `name` FROM (
SELECT `name`, COUNT(*) AS `count` FROM `moods`
WHERE `mood` IN ('Excited', 'Happy', 'Proud') GROUP BY `name`
) WHERE `count` = 3
Alternatively, you can use join: 或者,您可以使用join:
SELECT `m1`.`name` FROM `moods` `m1`
JOIN `moods` `m2` USING (`name`)
JOIN `moods` `m3` USING (`name`)
WHERE `m1`.`mood` = 'Excited' AND `m2`.`mood` = 'Happy' AND `m3`.`mood` = 'Proud'
Not so cute, but might be faster if you use LIMIT. 不太可爱,但是如果使用LIMIT可能会更快。 Or maybe not. 或者可能不是。 Depends a lot on query planner. 在很大程度上取决于查询计划器。
UPD : thanks to Tudor Constantin for reminding me about HAVING, the first query can then be: UPD :感谢Tudor Constantin提醒我有关HAVING的信息,因此第一个查询可以是:
SELECT `name` FROM `moods`
WHERE `mood` IN ('Excited', 'Happy', 'Proud')
GROUP BY `name`
HAVING COUNT(*)>3
If I understand your question correctly, I think in this case you don't actually need a UNION
but just multiple conditions in a WHERE
statement. 如果我正确理解了您的问题,我认为在这种情况下,您实际上并不需要UNION
而只需要WHERE
语句中的多个条件即可。
Try this: 尝试这个:
SELECT name, mood FROM myTable WHERE mood in ('Happy','Excited','Proud')
This should give the result: 这应该给出结果:
John, Happy
John, Excited
John, Proud
If you don't care about getting multiple results for mood, try this: 如果您不希望因心情而获得多个结果,请尝试以下操作:
SELECT DISTINCT name FROM myTable WHERE mood in ('Happy','Excited','Proud')
Which will just give: 这将给:
This should give the result: 这应该给出结果:
John
Updated If you want to match ALL the conditions, you'd probably have to use subselects: 已更新如果要匹配所有条件,则可能必须使用子选择:
SELECT DISTINCT name FROM myTable
WHERE name IN
(SELECT name FROM myTable WHERE mood = 'Happy')
AND name IN
(SELECT name FROM myTable WHERE mood = 'Excited')
AND name IN
(SELECT name FROM myTable WHERE mood = 'Proud')
Try with: 尝试:
SELECT name FROM user_moods um1
INNER JOIN user_moods um2 ON um1.name = um2.name
WHERE um1.mood IN ('Happy','Excited','Proud')
GROUP BY um2.mood
HAVING COUNT(um2.mood) = 3 # the number of different moods
your query is already correct. 您的查询已经正确。 you can ADD
an extra column using you searched word in the WHERE
clause. 您可以在WHERE
子句中使用搜索到的词来ADD
额外的列。
SELECT name,'Happy' as imood WHERE mood='Happy'
UNION
SELECT name,'Excited' as imood WHERE mood='Excited'
UNION
SELECT name,'Proud' as imood WHERE mood='Proud'
用INTERSECT替换UNION。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.