如何将 MySQL 结果转换为字符串?

发布于 2024-08-19 14:05:02 字数 369 浏览 9 评论 0原文

有一个查询:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空名 2024-08-26 14:05:02

切勿在一个单元格中存储多个值。 blatable 中的每个值都应位于其自己的行中,然后您的 IN 子句将发挥作用。查看数据库规范化,尤其是第一范式,了解如何设计表。

由于您将所有值都放在一个单元格中,因此执行 IN 比较会导致所有用户 ID 与字符串“2,12,29,714,543,1719”进行比较,这显然不匹配。您的查询实际上如下所示:

...FROM stolencards WHERE userid IN ("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 your IN 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:

...FROM stolencards WHERE userid IN ("2,12,29,714,543,1719")
季末如歌 2024-08-26 14:05:02

检查blalist和userid的数据类型是否相同,或者blalist真的返回userid?

check the data type of blalist and userid are same, or blalist really returns userid?

如果您指定了正确的数据类型以及 pk 和 fk 关系,请尝试以下操作:

SELECT s.userid, s.name, s.surname, s.creditcardnum, s.items
FROM stolencards s inner join blatable b on s.userid = b.blafield
where b.blafield = 714

If you have specified correct data types and pk and fk relationships then try this:

SELECT s.userid, s.name, s.surname, s.creditcardnum, s.items
FROM stolencards s inner join blatable b on s.userid = b.blafield
where b.blafield = 714
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文