SELECT * FROM table1
WHERE col1 = ANY (
SELECT DISTINCT col2 FROM table2
WHERE first = 'blah'
OR second = 'blahblah'
OR third = 'blahblahblah' );
子查询检查三列(first
、second
、third
)。在此示例中,我使用 ANY
,但我希望它像这样返回:
If found `first` return `first`
else if found `second` return `second`
else if found `third` return `third`
知道我在说什么吗?我只能选择优先级为 first
、second
、third
最佳答案
我想你想要这个:
SELECT * FROM table1
WHERE col1 = (
SELECT col2 FROM table2
WHERE first = 'blah'
OR second = 'blahblah'
OR third = 'blahblahblah'
ORDER BY
first = 'blah' DESC,
second = 'blahblah' DESC,
third = 'blahblahblah' DESC
LIMIT 1
);
关于mysql - 从子查询中选择 "the best result"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28350324/