简体   繁体   English

嵌套在 mysql 中的 Select

[英]Nested Select in mysql

Is it possible to create a nested select statement something like this in mysql?是否可以在 mysql 中创建类似这样的嵌套 select 语句?

SELECT * from myTable WHERE id = (SELECT id from data where dataId = 1);

If this is not the correct way to formulate such a statement could anyone point me in the right direction for the way in which it should be formulated.如果这不是制定此类声明的正确方法,那么任何人都可以为我指明制定该声明的正确方向。

Thanks谢谢

SELECT * from myTable WHERE id IN (SELECT id from data where dataId = 1);
                                ^---- You should use in rather than =

This syntax is fine.这个语法很好。 Note though that this will only work if there is one data with dataId 1. If there is likely to be multiple ids returned by the inner select then it is better to use:请注意,这仅在只有一个数据的 dataId 为 1 时才有效。如果内部 select 可能返回多个 ID,则最好使用:

SELECT * from myTable WHERE id IN (SELECT id from data where dataId = 1);

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

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