Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用mysql 3连接查询获取完整数据_Mysql_Sql - Fatal编程技术网

无法使用mysql 3连接查询获取完整数据

无法使用mysql 3连接查询获取完整数据,mysql,sql,Mysql,Sql,我在domains表中有1500条记录,但使用此查询只能得到1215条记录。如何修改此查询以提供所需的结果和更好的性能 SELECT d.id, d.domain_name, d.action, d.comment, d.agent_email, d.assigned_date, d.added_date, dd.registered_on, dd.expiry_date, dd.updated_date, dd.acquire_price, dd.acquire_date, d

我在domains表中有1500条记录,但使用此查询只能得到1215条记录。如何修改此查询以提供所需的结果和更好的性能

SELECT 
  d.id, d.domain_name, d.action, d.comment, d.agent_email, 
  d.assigned_date, d.added_date, dd.registered_on, dd.expiry_date,
  dd.updated_date, dd.acquire_price, dd.acquire_date, dd.email,
  dd.effective_price, dd.registrar, dd.status, dd.servers, 
  count(l.lead_domain), d.domainer_email, d.current_status, 
  d.undeveloped, d.sedo, d.afternic, d.flippa, d.uniregistry, 
  d.go_daddy, d.domr, d.minimum_offer, d.buy_it_now_price, 
  d.way_to_find_leads, dd.tlds_taken 
FROM domains d 
right join domains_data dd on d.domain_name=dd.domain_name 
left outer join lead_domains l on d.domain_name=l.domain_name 
  and d.domainer_email=l.domainer_email 
group by d.domain_name 
having d.action='all' and d.domainer_email='abc@gmail.com' 
order by d.added_date desc;

我的建议是,在加入其他表之前先过滤

SELECT d.id
    ,d.domain_name
    ,d.action
    ,d.comment
    ,d.agent_email
    ,d.assigned_date
    ,d.added_date
    ,dd.registered_on
    ,dd.expiry_date
    ,dd.updated_date
    ,dd.acquire_price
    ,dd.acquire_date
    ,dd.email
    ,dd.effective_price
    ,dd.registrar
    ,dd.status
    ,dd.servers
    ,t1.cnt
    ,d.domainer_email
    ,d.current_status
    ,d.undeveloped
    ,d.sedo
    ,d.afternic
    ,d.flippa
    ,d.uniregistry
    ,d.go_daddy
    ,d.domr
    ,d.minimum_offer
    ,d.buy_it_now_price
    ,d.way_to_find_leads
    ,dd.tlds_taken 
FROM domains d 
inner join
    (select domain_name, count(1) as cnt 
        from domain
        where
            action='all' and domainer_email='abc@gmail.com'
        group by domain_name
     ) as t1 on t1.domain_name = d.domain_name
right join domains_data dd on d.domain_name=dd.domain_name 
left outer join lead_domains l on d.domain_name=l.domain_name and d.domainer_email=l.domainer_email 
order by d.added_date desc;

您的结果取决于
domains\u数据

由于您正确地将
连接到
域_数据
,因此将获取
域_数据
中的条目。如果要考虑
域的条目
,请使用左连接,如下所示

SELECT d.id, d.domain_name, d.action, d.comment, d.agent_email, d.assigned_date, d.added_date, dd.registered_on, dd.expiry_date, dd.updated_date, dd.acquire_price, dd.acquire_date, dd.email, dd.effective_price, dd.registrar, dd.status, dd.servers, COUNT(l.lead_domain), d.domainer_email, d.current_status, d.undeveloped, d.sedo, d.afternic, d.flippa, d.uniregistry, d.go_daddy, d.domr, d.minimum_offer, d.buy_it_now_price, d.way_to_find_leads, dd.tlds_taken
FROM domains d
LEFT JOIN domains_data dd ON d.domain_name=dd.domain_name
LEFT OUTER JOIN lead_domains l ON d.domain_name=l.domain_name AND d.domainer_email=l.domainer_email
GROUP BY d.domain_name
HAVING d.action='all' AND d.domainer_email='abc@gmail.com'
ORDER BY d.added_date DESC;

如果您仍然无法获得所需的结果,请检查
是否具有
条件。

@Harshal根据我的理解,我认为您提供的查询将无法正常工作,因为没有正确遵循分组规则

SELECT  d.id,
        d.domain_name,
        d.action,
        d.comment,
        d.agent_email,
        d.assigned_date,
        d.added_date,
        dd.registered_on,
        dd.expiry_date,
        dd.updated_date,
        dd.acquire_price,
        dd.acquire_date,
        dd.email,
        dd.effective_price,
        dd.registrar,
        dd.status,
        dd.servers,
        (SELECT COUNT(lead_domain) FROM lead_domains WHERE domain_name = d.domain_name AND domainer_email = d.domainer_email) AS lead_domain,
        d.domainer_email,
        d.current_status,
        d.undeveloped,
        d.sedo,
        d.afternic,
        d.flippa,
        d.uniregistry,
        d.go_daddy,
        d.domr,
        d.minimum_offer,
        d.buy_it_now_price,
        d.way_to_find_leads,
        dd.tlds_taken
FROM domains d
LEFT JOIN domains_data dd 
       ON d.domain_name = dd.domain_name
WHERE d.action = 'all'
      AND d.domainer_email = 'abc@gmail.com'
ORDER BY
    d.added_date DESC;

由于您没有提供任何预期的结果,我只是猜测我的解决方案会起作用

以表格格式提供示例数据和预期输出Right JOIN和LEFT JOIN的组合。。。那太过分了。大多数人发现
主表左连接可选数据
可选数据右连接主表
更容易理解。(顺便说一句,由于HAVING子句,您的RIGHT JOIN仍然返回内部连接结果。)除非处于兼容模式,否则GROUP BY无效,不会在较新的MySQL版本上运行。(您通常按照所选的相同列进行分组,但设置函数的参数除外。)感谢它对我有效,而且获取数据的时间也减少了需要在having子句中进行更改,但我只需使用两个字段来过滤数据在我的句子前面有一个
If