我有这个查询工作正常。没有错误或者是什么。但它似乎不适用于最后一部分,并且不会填充contact_data cd中的最后三个字段。
select c.*,cd.* FROM
(select * from
(select con.id_contact as v,substr(concat(ifnull(con.firstname,''),ifnull(con.lastname,'')),1,4) as s0, null as s1, null as s2
from contact con where id_user=123 and firstname != '' and firstname != ' ' and firstname is not null) as tbl235768 where 1=1 AND v IN
(select v from
(select * from
(select con.id_contact as v,substr(concat(ifnull(con.firstname,''),ifnull(con.lastname,'')),1,4) as s0, null as s1, null as s2
from contact con where id_user=123 and email = '') as tbl235770 where 1=1) as tblAnd) AND v IN
(select v from
(select * from (select con.id_contact as v,substr(concat(ifnull(con.firstname,''),ifnull(con.lastname,'')),1,4) as s0, null as s1, null as s2
from contact con where id_user=123 and mobile != '' and mobile != ' ' and mobile is not null) as tbl235772 where 1=1) as tblAnd)) as tblBucket
left join contact c on c.id_contact=v left join
(select id_contact, group_concat(name) as custom_names,group_concat(value) as custom_values FROM contact_data where
id_user=20 group by id_contact) as cd on cd.id_contact=c.id_contact group by (c.id_contact)
但是,如果我只运行以下
的最后一部分 select id_contact, group_concat(name) as custom_names,group_concat(value) as custom_values
FROM contact_data where id_user=798 group by id_contact
它给了我想要的结果。我的查询有什么问题?任何帮助都会非常感激,谢谢。
编辑:我在得到几个答案后正在编辑我的问题。
我删除了所有嵌套部件,但仍然没有运气。
select c.*,cd.* FROM (select * from (select con.id_contact as v,substr(concat(ifnull(con.firstname,''),ifnull(con.lastname,'')),1,4) as s0, null as s1, null as s2 from contact con where id_user=10879) as tbl235785
where 1=1) as tblBucket left join contact c on c.id_contact=v left join (select id_contact, group_concat(name) as custom_names,group_concat(value) as custom_values
FROM contact_data where id_user=798 group by id_contact) as cd on cd.id_contact=c.id_contact group by (c.id_contact)
答案 0 :(得分:2)
这是你的sql更简单:
所有1 = 1和SELECT * FROM(子查询)都可以消失 - 他们什么都不做。然后逻辑变得清晰 - 你只需要在where语句而不是子查询中添加一些附加条款。我认为当它清理完毕后你可以看到 - 在第一个选择中你有id_user=123
,在第二个选择中你有id_user=20
。
可能希望那些是一样的?你甚至不需要第二个加入选择中的where子句。由于它已加入,您应该只在外部查询中加入此字段的值。
select c.*,cd.*
FROM (select con.id_contact as v,substr(concat(ifnull(con.firstname,''),ifnull(con.lastname,'')),1,4) as s0, null as s1, null as s2
from contact con
where id_user=123 and
firstname != '' and firstname != ' ' and firstname is not null
and
email = ''
OR
(
mobile != '' and mobile != ' ' and mobile is not null
)
) as tbl235768
left join contact c on c.id_contact=v
left join (select id_contact, group_concat(name) as custom_names,group_concat(value) as custom_values
FROM contact_data
where id_user=20
group by id_contact
) as cd on cd.id_contact=c.id_contact
group by (c.id_contact)
答案 1 :(得分:1)
在一个地方,你在id_user = 10879匹配,然后在id_user = 798匹配的不同子查询中,然后你加入两个,因为id_user字段都在查看不同的数字,你得不到你的匹配加入。