【问题标题】:Sql query showing to many values in one columnSql 查询在一列中显示多个值
【发布时间】:2018-02-18 14:42:07
【问题描述】:

列 t.name(genre) 显示许多相同的值我试图让它工作,但我没有成功..

这是我的 SQL

select o.id, b.title, a.firstName, a.lastName, 
        b.noOfPages, b.price, group_concat(t.name) as 'genre'
from author a,book_author ba, book_type bt,orders o,type t, book b, order_list ol
where ol.book_fk = b.id 
and bt.book_fk = b.id 
and bt.type_fk = t.id 
and ba.author_fk = a.id 
and ba.book_fk = b.id 
and ol.orders_fk = '74' 
GROUP BY ol.id

图片显示列类型查询返回多少个值

【问题讨论】:

  • 提示:从不FROM 子句中使用逗号。 始终使用正确、明确的JOIN 语法。
  • 请编辑您的问题并向我们展示示例数据和预期输出。请注意,您使用GROUP BY 没有多大意义,因为您选择的是非聚合列,而title 甚至可能不是其自己表中的主键。
  • 嗨。你的问题是什么?当您编辑您的帖子以询问它时,请确保您告诉我们您的查询应该返回什么,以及为什么您得到的是错误/有问题的。 minimal reproducible example 请。 PS请use text, not images/links, for text (including code, tables & ERDs)。使用图片只是为了方便补充文本和/或无法在文本中给出的内容。
  • 我编辑了它(列 t.name 显示了许多相同的值,我试图让它工作,但我没有成功..)
  • 图片显示有多少列类型返回值

标签: php mysql sql phpmyadmin


【解决方案1】:

我相信您担心您的类型列中“犯罪”的重复值。 尝试在 group_concat 中使用 distinct。

select o.id, b.title, a.firstName, a.lastName,b.noOfPages, b.price,
group_concat(distinct t.name) as 'genre' from author a,book_author ba,
book_type bt,orders o,type t, book b, order_list ol where ol.book_fk = b.id
and bt.book_fk = b.id  and bt.type_fk = t.id and ba.author_fk = a.id
and ba.book_fk = b.id  and ol.orders_fk = '74' GROUP BY ol.id;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 2013-07-07
    • 1970-01-01
    • 2021-08-20
    相关资源
    最近更新 更多