我有一个sql语句如下:
SELECT s.sp_id,s.synopsis,s.logline,s.user_id,s.bin_status,s.sp_order,s.sp_down_count,s.sp_name,
`users`.firstname as `firstname`,`users`.lastname as `lastname`,s.status,
`users`.email as `email`,`users`.phone as `phone`,`users`.address1 as `address1`,
`users`.address2 as `address2`,`users`.`r_group`, view_coverages.grade,coverages.comments
FROM s
LEFT JOIN `users` ON `users`.user_id=s.user_id
LEFT JOIN view_coverages ON view_coverages.sp_id=s.sp_id
LEFT JOIN `coverages` ON `coverages`.sp_id=s.sp_id
WHERE $email_filter lower(s.synopsis) like '%$search_word_fix%'
OR lower(s.sp_name) like '%$search_word_fix%'
OR lower(s.logline) like '%$search_word_fix%'
OR lower(coverages.comments) like '% $search_word_fix %'
ORDER BY view_coverages.grade DESC
当我显示结果时,我得到了很多重复。 我想显示搜索结果,其中s.sp_name不重复。
答案 0 :(得分:2)
您有两种选择:
SELECT
更改为SELECT DISTINCT
,只选择唯一且不重复的结果。GROUP BY s.sp_name
但您需要在所选的所有其他字段上执行聚合功能。由于我确定您无法对某人的姓名,地址,电话号码等进行平均或求和,因此您应该尝试选项1(SELECT DISTINCT
)。如果您的重复行中有非唯一字段,则除了唯一字段外,您需要GROUP BY
每个字段;在剩余的唯一字段上使用聚合函数(即SUM
,AVG
等)。
答案 1 :(得分:1)
Ty this:
SELECT s.sp_id,s.synopsis,s.logline,s.user_id,s.bin_status,s.sp_order,s.sp_down_count,s.sp_name,
`users`.firstname as `firstname`,`users`.lastname as `lastname`,s.status,
`users`.email as `email`,`users`.phone as `phone`,`users`.address1 as `address1`,
`users`.address2 as `address2`,`users`.`r_group`, view_coverages.grade,coverages.comments
FROM s
LEFT JOIN `users` ON `users`.user_id=s.user_id
LEFT JOIN view_coverages ON view_coverages.sp_id=s.sp_id
LEFT JOIN `coverages` ON `coverages`.sp_id=s.sp_id
WHERE $email_filter lower(s.synopsis) like '%$search_word_fix%'
OR lower(s.sp_name) like '%$search_word_fix%'
OR lower(s.logline) like '%$search_word_fix%'
OR lower(coverages.comments) like '% $search_word_fix %'
ADD ---> group by s.sp_name
ORDER BY view_coverages.grade DESC