gpt4 book ai didi

mysql - SQL 查询未显示所需结果

转载 作者:太空宇宙 更新时间:2023-11-03 11:58:02 25 4
gpt4 key购买 nike

我的这个查询运行良好。没有错误或其他任何东西。但它似乎不适用于最后一部分,并且不会填充 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)

最佳答案

这里是你的 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)

关于mysql - SQL 查询未显示所需结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31167300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com