Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.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三表联接结果空集_Mysql_Sql_Outer Join_Right Join - Fatal编程技术网

Mysql三表联接结果空集

Mysql三表联接结果空集,mysql,sql,outer-join,right-join,Mysql,Sql,Outer Join,Right Join,您好,我已经加入了三个表,但它返回空结果,即使有一些结果。这是我的sql SELECT c.code,c.name, a.ltp as begning, b.ltp as enddate, d.interim_cash,d.interim_rec_date, CAST(((b.ltp - a.ltp) / a.ltp * 100) AS DECIMAL(10, 2)) as chng FROM eod_stock a JOIN eod_stock b ON a.company_id = b.co

您好,我已经加入了三个表,但它返回空结果,即使有一些结果。这是我的sql

SELECT c.code,c.name, a.ltp as begning, b.ltp as enddate, d.interim_cash,d.interim_rec_date,
CAST(((b.ltp - a.ltp) / a.ltp * 100) AS DECIMAL(10, 2)) as chng
FROM eod_stock a
JOIN eod_stock b ON a.company_id = b.company_id
LEFT OUTER JOIN company AS c
ON c.ID = a.company_id
RIGHT JOIN divident_info AS d
ON c.ID = d.company_id
WHERE a.entry_date = "2012-09-24"
AND b.entry_date = "2012-09-25"
AND d.interim_rec_date BETWEEN "2012-09-24" AND "2012-09-25"
AND a.company_id IN (13, 2)
AND d.company_id IN (13,2);
我期待的结果是这样的:

+--------+-----------------+---------+--------+--------+------------------+------------+
| code   | name            | begning | end    | chng   | interim_rec_date |interim_cash|
+--------+-----------------+---------+--------+--------+------------------+------------+
| ABBANK | AB BANK LIMITED |  518.00 | 459.00 | -11.39 |2012-09-24        |10          |
| 1STICB | 1ST ICB M.F.    |  227.00 | 253.00 |  11.45 |                  |            |
+--------+-----------------+---------+--------+--------+------------------+------------+
WHERE a.entry_date = "2012-09-24" AND
      b.entry_date = "2012-09-25" AND
      d.interim_rec_date BETWEEN "2012-09-24" AND "2012-09-25" AND
      a.company_id IN (13, 2) AND
      d.company_id IN (13, 2) AND
      (a.entry_date is null  or a.company_id is null or b.entry_date is null)
但是我在结果中得到了空集,这是因为第二个中间信息是0吗?我怎样才能得到像上面这样的所有信息?如果行是空的,那么它可以是空的,但我需要与它相关的其他信息

以下是我的表模式:

爆炸品库存:

+-----------------+------------------+------+-----+---------+-------+
| Field           | Type             | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+-------+
| company_id      | varchar(30)      | NO   | PRI | NULL    |       |
| entry_date      | date             | NO   | PRI | NULL    |       |
| entry_timestamp | int(10) unsigned | NO   |     | NULL    |       |
| open            | decimal(16,2)    | NO   |     | NULL    |       |
| high            | decimal(16,2)    | NO   |     | NULL    |       |
| low             | decimal(16,2)    | NO   |     | NULL    |       |
| ltp             | decimal(16,2)    | NO   |     | NULL    |       |
| ycp             | decimal(16,2)    | NO   |     | NULL    |       |
| cse_price       | decimal(9,2)     | NO   |     | NULL    |       |
| cse_volume      | decimal(18,2)    | NO   |     | NULL    |       |
| total_trade     | int(30)          | NO   |     | NULL    |       |
| total_volume    | int(30)          | NO   |     | NULL    |       |
| total_value     | decimal(18,4)    | NO   |     | NULL    |       |
+-----------------+------------------+------+-----+---------+-------+
divident信息:

+------------------+--------------+------+-----+-------------------+-----------------------------+
| Field            | Type         | Null | Key | Default           | Extra                       |
+------------------+--------------+------+-----+-------------------+-----------------------------+
| divident_ID      | int(11)      | NO   | PRI | NULL              | auto_increment              |
| company_id       | int(11)      | NO   |     | NULL              |                             |
| year             | year(4)      | NO   |     | NULL              |                             |
| right_base       | int(11)      | NO   |     | NULL              |                             |
| right_new        | int(11)      | NO   |     | NULL              |                             |
| right_dec_date   | date         | NO   |     | NULL              |                             |
| right_rec_date   | date         | NO   |     | NULL              |                             |
| interim_cash     | decimal(6,2) | NO   |     | NULL              |                             |
| interim_stock    | decimal(8,2) | NO   |     | NULL              |                             |
| interim_dec_date | date         | NO   |     | NULL              |                             |
| interim_rec_date | date         | NO   |     | NULL              |                             |
| annual_cash      | decimal(6,2) | NO   |     | NULL              |                             |
| annual_stock     | decimal(8,2) | NO   |     | NULL              |                             |
| annual_dec_date  | date         | NO   |     | NULL              |                             |
| annual_rec_date  | date         | NO   |     | NULL              |                             |
| update_time      | timestamp    | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+------------------+--------------+------+-----+-------------------+-----------------------------+
请帮我查一下结果好吗?

这是您的查询:

SELECT c.code,c.name, a.ltp as begning, b.ltp as enddate, d.interim_cash,d.interim_rec_date,
       CAST(((b.ltp - a.ltp) / a.ltp * 100) AS DECIMAL(10, 2)) as chng
FROM eod_stock a JOIN
     eod_stock b
     ON a.company_id = b.company_id LEFT OUTER JOIN
     company AS c
     ON c.ID = a.company_id RIGHT JOIN
     divident_info d
     ON c.ID = d.company_id
WHERE a.entry_date = "2012-09-24" AND
      b.entry_date = "2012-09-25" AND
      d.interim_rec_date BETWEEN "2012-09-24" AND "2012-09-25" AND
      a.company_id IN (13, 2) AND
      d.company_id IN (13, 2);
就个人而言,我发现很难跟踪
left join
s与
right join
s混合的查询:只使用
left join
s创建结构要容易得多,这样您就可以很容易地看到哪个表驱动了查询

在任何情况下,
where
子句都会撤消外部联接的效果。除了您明确列出的条件外,您还表示:

a.entry_date is not null and
b.entry_date is not null and
d.interim_rec_date is not null and
a.company_id is not null and
d.company_id is not null
很难说需要消除哪些条件才能得到想要的结果。我能说的是,其中一些应该放在
on
子句中,而不是
where
子句中。这是最好的解决办法。您还可以将
where
更改为如下内容:

+--------+-----------------+---------+--------+--------+------------------+------------+
| code   | name            | begning | end    | chng   | interim_rec_date |interim_cash|
+--------+-----------------+---------+--------+--------+------------------+------------+
| ABBANK | AB BANK LIMITED |  518.00 | 459.00 | -11.39 |2012-09-24        |10          |
| 1STICB | 1ST ICB M.F.    |  227.00 | 253.00 |  11.45 |                  |            |
+--------+-----------------+---------+--------+--------+------------------+------------+
WHERE a.entry_date = "2012-09-24" AND
      b.entry_date = "2012-09-25" AND
      d.interim_rec_date BETWEEN "2012-09-24" AND "2012-09-25" AND
      a.company_id IN (13, 2) AND
      d.company_id IN (13, 2) AND
      (a.entry_date is null  or a.company_id is null or b.entry_date is null)
但是,您可能只需要这些条件中的一个或两个。注意
d
上的条件应保留在
where
子句中,因为
右外部联接将所有行保留在
d
中,因此不会为
NULL
(除非原始数据中
NULL

尝试以下操作:

SELECT c.code,c.name, a.ltp as begning, b.ltp as enddate, 
d.interim_cash,d.interim_rec_date,
CAST(((b.ltp - a.ltp) / a.ltp * 100) AS DECIMAL(10, 2)) as chng
FROM eod_stock a
JOIN eod_stock b ON a.company_id = b.company_id
LEFT OUTER JOIN company AS c
ON c.ID = a.company_id
RIGHT JOIN divident_info AS d
ON c.ID = d.company_id AND d.interim_rec_date BETWEEN "2012-09-24" AND "2012-09-25"
AND d.company_id IN (13,2)    
WHERE a.entry_date = "2012-09-24"
AND b.entry_date = "2012-09-25"
AND a.company_id IN (13, 2);

最后,我得到了我需要的查询:

 SELECT c.code,c.name, a.ltp as begning, b.ltp as enddate, d.interim_cash,d.interim_rec_date,      d.annual_rec_date,d.annual_cash,
 CAST(((b.ltp - a.ltp) / a.ltp * 100) AS DECIMAL(10, 2)) as chng
 FROM eod_stock AS a

 LEFT OUTER JOIN eod_stock AS b
 ON a.company_id = b.company_id

 LEFT OUTER JOIN company AS c
 ON c.ID = a.company_id

 LEFT OUTER JOIN dividend_info AS d
 ON c.ID = d.company_id AND d.interim_rec_date BETWEEN "2012-09-24" AND "2012-09-25" AND       d.annual_rec_date BETWEEN "2012-09-24" AND "2012-09-25"

 WHERE a.entry_date = "2013-09-24"
 AND b.entry_date = "2013-09-25"
 AND a.company_id IN (13, 2,4,5);

感谢Gordon Linoff为我带来了我刚刚搞乱的正确加入的轨道

这个“结局”怎么样在您的请求中,这似乎是一个错误的解释。在不同的查询中,该端没有产生任何问题,而是更新了,但没有luckConsider将问题简化为仅相关列,并为此提供适当的DDL和/或SQLFIDLE,以及所需的结果集。它将另一个结果保留为空。平均公司id不返回结果