表一
customer_name restaurant_id visits
shubham 1 4
nayan 2 6
表2
restaurant_id restaurant_name
1 ramon hux
2 Mt.Pete's
问题:打印访问次数大于 3 的顾客姓名和餐厅名称应该有空格还是单引号?
我的回答:
select customer_name from table1 where id=(select id from table2
where restaurant_name like "% %" or restaurant_name like "%'%") and visits>3;
问题:子查询返回多于 1 行;
我该如何解决这个问题?
最佳答案
尝试使用IN
子句:
select customer_name from table1 where id IN (select id from table2
where restaurant_name like "% %" or restaurant_name like "%'%") and visits>3;
您的子查询返回超过 1 个 ID,因此您的查询失败。您需要使用 IN 来合并所有 id。
关于MySQL 从表中选择名称,比较来自相关表的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38612225/