如果 1 列不在结果中,则附加到 MySQL 结果
我想创建一个类似于
SELECT person, city FROM mytable
UNION
SELECT 'BOB', 'Chicago' IF 'BOB' NOT IN (SELECT person FROM mytable);
“如果结果中未返回“BOB”,我想将他附加到结果中并将他列为在芝加哥”的 MyQSL 查询。如果 BOB 确实出现在结果中,无论他的位置在哪里,我都不想将他添加为在芝加哥。
如果我完全匹配这些列,我就可以完成这项工作,但如果 BOB 被列为芝加哥以外的其他地方,我最终会得到多个结果。
SELECT person, city FROM mytable
UNION
SELECT 'BOB', 'Chicago'
但我不想在位置上进行匹配。只是这个人的名字。
I want to create a MyQSL Query similar to
SELECT person, city FROM mytable
UNION
SELECT 'BOB', 'Chicago' IF 'BOB' NOT IN (SELECT person FROM mytable);
If 'BOB' is not returned in the results, I want to append him to the results and list him as being in Chicago. If BOB does come back in the results, no matter what his location is, I do not want to append him as being in Chicago.
I can make this work if I exactly match the columns, but I will end up getting multiple results for BOB if he is listed as being somewhere other than Chicago.
SELECT person, city FROM mytable
UNION
SELECT 'BOB', 'Chicago'
but I do not want to match on the location. Just the person's name.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该有效:
更优化的版本,返回相同的结果
This should work:
A more optimized version, that returns the same results
这是对原始查询的重写,应该可以工作:
Here's a rewrite of your original query that should work: