简体   繁体   English

带有“where in”子句的嵌套 MySql Select 语句

[英]Nested MySql Select statement with “where in” clause

I'll try to detail this the best I can.我会尽力详细说明这一点。 I have a nested select statement with a where in clause, but the nested part of the select should be interpreted as a literal string (I believe this is the right terminology).我有一个带有 where in 子句的嵌套 select 语句,但是 select 的嵌套部分应该解释为文字字符串(我相信这是正确的术语)。 However the default behavior of mysql leads to a result I do not want.然而 mysql 的默认行为导致了我不想要的结果。

Ie IE

select class 
from cs_item 
where code="007"

+-------+
| class |
+-------+
| 1,3   |
+-------+

And the below is a query if I explicitly type "in (1,3)" as part of a select query:如果我在 select 查询中明确键入“in (1,3)”,则以下是一个查询:

select alpha,description 
from cs_quality 
where class in (1,3);

+-------+-------------+
| alpha | description |
+-------+-------------+
| STD   | STD         |
| XS    | XS          |
| 5     | Sch 5       |
| 10    | Sch 10      |
| 20    | Sch 20      |
| 40    | Sch 40      |
| 60    | Sch 60      |
| 80    | Sch 80      |
| 100   | Sch 100     |
| 120   | Sch 120     |
| 140   | Sch 140     |
| 160   | Sch 160     |
| XXS   | XXS         |
| 15L   | 150#        |
| 30L   | 300#        |
| 40L   | 400#        |
| 60L   | 600#        |
| 90L   | 900#        |
| 150L  | 1500#       |
| 200L  | 2000#       |
| 250L  | 2500#       |
| 300L  | 3000#       |
| 400L  | 4000#       |
| 600L  | 6000#       |
| 900L  | 9000#       |
+-------+-------------+

But when I go to nest this to get the same result I have...但是当我 go 嵌套这个以获得相同的结果时,我有......

select alpha,description 
from cs_quality 
where class in (select class from cs_item where code = "007")

+-------+-------------+
| alpha | description |
+-------+-------------+
| STD   | STD         |
| XS    | XS          |
| 5     | Sch 5       |
| 10    | Sch 10      |
| 20    | Sch 20      |
| 40    | Sch 40      |
| 60    | Sch 60      |
| 80    | Sch 80      |
| 100   | Sch 100     |
| 120   | Sch 120     |
| 140   | Sch 140     |
| 160   | Sch 160     |
| XXS   | XXS         |
+-------+-------------+

Which is just the part of "class in 1"... it balks on the ",3" component.这只是“class in 1”的一部分......它在“,3”组件上犹豫不决。 Is there a way for the nested select to be interpreted as literal text?有没有办法将嵌套的 select 解释为文字?

Thanks all, much appreciated.谢谢大家,非常感谢。 I had a bit of trouble wording this question but will edit as needed.我在措辞这个问题时遇到了一些麻烦,但会根据需要进行编辑。

Normalize, normalize, normalize your tables, in this case table cs_item .规范化、规范化、规范化您的表格,在本例中为表格cs_item You should NOT store multiple (comma separated) values in one field.您不应该在一个字段中存储多个(逗号分隔)值。

Until you do that, you can use:在你这样做之前,你可以使用:

select alpha, description 
from cs_quality 
where FIND_IN_SET( class , (select class from cs_item where code = '007'))

or或者

select q.alpha, q.description 
from cs_quality AS q
  join cs_item AS i
    on FIND_IN_SET( q.class , i.class )
where i.code = '007'

But this kind of using special functions instead of equality for JOINs, leads to very slow queries.但是这种使用特殊函数而不是 JOIN 的相等性会导致查询速度非常慢。 Storing comma separated lists leads to a ton of other problems.存储逗号分隔的列表会导致大量其他问题。 See here:看这里:

Short answer is: Yeah, it's that bad .简短的回答是:是的,就是这么糟糕

Your query needs to return multiple rows like this:您的查询需要返回多行,如下所示:

+-------+
| class |
+-------+
|   1   |
+-------+
|   3   |
+-------+

Or else it is as if you are doing:否则就好像你在做:

select alpha,description 
from cs_quality 
where class in ("1, 3");

Which you do not want.你不想要的。

Better use join , instead of a nested query更好地使用join ,而不是嵌套查询

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

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