当我运行以下mysql语句时,我返回 127行:
SELECT t.strat_id, t.candle_id, t.spot_entry, t.spot_close, t.f,t.fa,t.b,t.we,t.direct,t.date_created,t.date_closed,t.notes,t.results
FROM cp_user_trades AS t
INNER JOIN candles2 AS c
ON t.candle_id = c.id
WHERE t.user_login = "user"
AND t.active=0
AND t.deleted=0
ORDER BY t.date_closed DESC
当我删除INNER JOIN
后,我返回 131行:
SELECT t.strat_id, t.candle_id, t.spot_entry, t.spot_close, t.f,t.fa,t.b,t.we,t.direct,t.date_created,t.date_closed,t.notes,t.results
FROM cp_user_trades AS t
WHERE t.user_login = "user"
AND t.active=0
AND t.deleted=0
ORDER BY t.date_closed DESC
我如何SELECT
第一个语句中未返回的 4行?
谢谢!
答案 0 :(得分:2)
假设c.ID是candles2表的主键:
使用左连接并添加c.Id为空的位置
答案 1 :(得分:1)
要查找表格之间的ID,我使用union all
和group by
:
select in_cut, in_c2, count(*) as cnt, min(candle_id), max(candle_id)
from (select candle_id, sum(in_cut) as in_cut, sum(in_c2) as in_c2
from ((select candle_id, 1 as in_cut, 0 as in_c2
from cp_user_trades
) union all
(select id, 0, 1
from candles2
)
) cc
group by candle_id
) c
group by in_cut, in_c2;
这为您提供了两个表中的ID的三种可能性(在第一个,第二个和两个表中)。它显示了两个表中是否重复了id,并给出了id的示例。
答案 2 :(得分:0)
我的建议是使用返回131行的第二个查询,子查询使其不包含另一个查找的127个。
SELECT t.strat_id, t.candle_id, t.spot_entry, t.spot_close, t.f,t.fa,t.b,t.we,t.direct,t.date_created,t.date_closed,t.notes,t.results
FROM cp_user_trades AS t
WHERE t.user_login = "user"
AND t.strat_id NOT IN (SELECT t.strat_id
FROM cp_user_trades AS t
INNER JOIN candles2 AS c
ON t.candle_id = c.id
WHERE t.user_login = "user"
AND t.active=0
AND t.deleted=0
ORDER BY t.date_closed DESC)
AND t.active=0
AND t.deleted=0
ORDER BY t.date_closed DESC
我认为t.strat_id会被排除。我不知道主要(唯一)密钥是什么,但是如果那个重要的话,你可以用t.candle_id代替行AND t.strat_id NOT IN (SELECT t.strat_id
。还有其他类型的连接,这肯定比这更有效。