简体   繁体   English

SQL查询未显示所需结果

[英]SQL query not displaying the desired result

I have this query which works fine. 我有这个查询工作正常。 No errors or what soever. 没有错误或任何错误。 But it doesn't seem to work with last part and doesn't populate the last three fields from contact_data cd. 但这似乎不适用于最后一部分,也不会填充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)

But, if i run the last part only which is the following 但是,如果我只运行最后一部分,则如下

 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

It gives me desired result. 它给了我想要的结果。 what is wrong with my query? 我的查询出了什么问题? Any help would be greatly apprecited, Thanks. 任何帮助将不胜感激,谢谢。

Edit : i am editing my question after getting a few answers. 编辑:我正在得到一些答案后编辑我的问题。

I have removed all the nested parts but still no luck. 我删除了所有嵌套部分,但仍然没有运气。

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)

Here is your sql made simpler: 这是使您的SQL更简单的方法:

All of the 1=1 and SELECT * FROM (sub-query) can go away -- they don't do anything. 所有1 = 1和SELECT * FROM(子查询)都可以消失-它们什么也不做。 Then the logic becomes clear -- you just need some additional clauses on your where statement instead of the sub-queries. 这样逻辑就会变得很清楚-您只需要在where语句上有一些附加子句即可,而不是子查询。 I think when it is cleaned up like this you can see - in the first select you have id_user=123 and in the second you have id_user=20 . 我认为,当像这样进行清理时,您可以看到-在第一个选择中您具有id_user=123 ,在第二个中您具有id_user=20

Probably want those to be the same? 可能希望它们相同吗? You don't even need the need the where clause in the 2nd joined select. 您甚至不需要第二个联接的select中的where子句。 Since it is joined you should just join this field the value in the outer query. 由于已联接,因此您只需要将该字段的值联接到外部查询中即可。

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)

在一个地方,您要在id_user = 10879中进行匹配,然后在不同的子查询中,您在id_user = 798进行匹配,然后将两者相结合,并且由于id_user字段都在查找不同的数字,因此在联接中没有匹配项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM