如何将 MySQL 结果转换为字符串?
有一个查询:
SELECT blalist FROM blatable WHERE blafield=714
它返回一个如下所示的字符串:“2,12,29,714,543,1719”。还有另一个查询:
SELECT userid, name, surname, creditcardnum, items
FROM stolencards WHERE userid IN
(SELECT blalist FROM blatable WHERE blafield=714)
现在这不起作用。
我只能通过单独执行这些查询来使其工作。我应该怎么做才能将其保留在单个查询中?
There is a query:
SELECT blalist FROM blatable WHERE blafield=714
which returns a string that looks like: "2,12,29,714,543,1719". And there is another query:
SELECT userid, name, surname, creditcardnum, items
FROM stolencards WHERE userid IN
(SELECT blalist FROM blatable WHERE blafield=714)
Now that's not working.
I only managed to get it working by executing these queries separately. What should I do to keep it in a single query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
切勿在一个单元格中存储多个值。
blatable
中的每个值都应位于其自己的行中,然后您的IN
子句将发挥作用。查看数据库规范化,尤其是第一范式,了解如何设计表。由于您将所有值都放在一个单元格中,因此执行
IN
比较会导致所有用户 ID 与字符串“2,12,29,714,543,1719”进行比较,这显然不匹配。您的查询实际上如下所示:You should never store more than one value in one cell. Each value in
blatable
should be in its own row, then yourIN
clause would work like a charm. Take a look at database normalization and especially at First normal form on how your tables should be designed.As you have all the values in one cell, doing an
IN
comparison results in all userids being compared to the string "2,12,29,714,543,1719", which obviously will not match. Your query effectively looks like this:检查blalist和userid的数据类型是否相同,或者blalist真的返回userid?
check the data type of blalist and userid are same, or blalist really returns userid?
如果您指定了正确的数据类型以及 pk 和 fk 关系,请尝试以下操作:
If you have specified correct data types and pk and fk relationships then try this: