2014-01-18 104 views
0

你好我有三個表,我已經加入,但它返回空結果,即使有一些結果假設。這裏是我的SQLMysql三表連接結果空集

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 |     |   | 
+--------+-----------------+---------+--------+--------+------------------+------------+ 

但我得到我的結果空集是這樣的,因爲第二個臨時信息爲0?我如何獲得像上面的所有信息,如果行是空的,那麼它可能是空白的,但我需要其他相關的信息。

這裏是我的表模式:

eod_stock:

+-----------------+------------------+------+-----+---------+-------+ 
| 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 |  | 
+-----------------+------------------+------+-----+---------+-------+ 

紅利信息:

+------------------+--------------+------+-----+-------------------+-----------------------------+ 
| 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 | 
+------------------+--------------+------+-----+-------------------+-----------------------------+ 

請你能幫我找出來的結果?

+0

你的請求中的這個「結束」如何被誤解? –

+0

在不同的查詢中,最終沒有產生任何問題,但更新,但沒有運氣 –

+0

考慮將問題簡化到只有相關的列,並提供適當的DDL和/或一個sqlfiddle,與期望的結果集 – Strawberry

回答

0

所以最後我得到了什麼,我需要在這裏查詢:

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); 

感謝戈登·利諾夫爲帶給我,我被搞亂與右連接在賽道上

1

這是您的查詢:

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是這樣的:

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子句中,因爲right outer join將保留所有行爲d,因此NULL(除非原始數據中爲NULL)。

+1

+1左右混合加入 – poke

0

試試這個:

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); 
+0

它將其他結果留空。意味着公司ID不會返回任何結果 –

+0

好的抱歉,無法給出正確的答案 –