简体   繁体   English

在mysql中查询select语句

[英]Query in mysql for select statement

Can we run below query in mysql 我们可以在mysql中的查询下面运行吗

SELECT rc.*,(SELECT * 
            from tbl_restaurantMenu 
            where menuCat = rc.id) 
as menu from tbl_restaurantMenuCat as rc;

If not how can we achieve similar result 如果没有,我们如何才能取得类似的结果

No, you can't do that. 不,你不能那样做。 When you use a subquery as a value, it must only return one column and one row, so you can't use SELECT * . 当使用子查询作为值时,它只能返回一列和一行,因此不能使用SELECT * You should use a JOIN instead. 您应该改用JOIN

SELECT *
FROM tbl_restaurantMenuCat AS rc
LEFT JOIN menu AS m ON rc.id = m.menuCat

I used LEFT JOIN so that this will show restaurants even if they have no matching menus. 我使用了LEFT JOIN ,这样即使没有匹配的菜单也可以显示餐馆。

Select * will not work this way. Select *将无法以这种方式工作。 You should go for JOIN . 您应该JOIN

Try below query: 请尝试以下查询:

SELECT rc.*,rm.*
FROM tbl_restaurantMenuCat rc
LEFT OUTER JOIN tbl_restaurantMenu rm ON rc.id = rm.menuCat

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM