从MySQL数据库的两个表中获取行 [英] Getting Rows From Two Tables from mysql database
本文介绍了从MySQL数据库的两个表中获取行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好...
我有两张桌子,就像一张是
uid name 1 peter 2 parker 3 jhon 4 watson 5 backinsale
还有另一个表:
uid date 1 2011-11-26 2 2011-11-26
在这里,我需要获取第二个表中没有uid的记录,就像
uid name 3 jhon 4 watson 5 backinsale
我写了类似
的查询
选择 * 来自 tbl1,tbl2 解决方案
尝试一下.
SELECT t2.uid, t2.name FROM t1 RIGHT JOIN t2 on t2.uid = t1.uid WHERE t2.date IS NOT NULL
问候,
爱德华(Eduard)
尝试使用左加入 [SELECT * FROM tbl1 左 JOIN tbl2 打开 tlb1.uid = tbl2.uid 位置 tbl2.uid = NULL
OP指出结果为空,他说对了,该声明应为:
SELECT tbl1.uid,tbl1.name FROM tbl1 左 JOIN tbl2 打开 tbl1.uid = tbl2.uid 位置 tbl2.uid IS NULL
我还更改了SELECT
,否则还会显示tbl2
的NULL
列.
尝试如下查询.
选择 * 来自表1 T1 其中 T1.uid 不是 in (选择 T2.uid 来自 Table2 T2);
>
hi all...
i have two tables like one is
uid name 1 peter 2 parker 3 jhon 4 watson 5 backinsale
and another table like:
uid date 1 2011-11-26 2 2011-11-26
here i need to get records in which uid is not there in second table that is like
uid name 3 jhon 4 watson 5 backinsale
i written query like
select * from tbl1,tbl2 where tbl1.uid!=tbl2.uid
but it is not return correct result set
can any one help me please
thanks in advance...
解决方案
try this one.
SELECT t2.uid, t2.name FROM t1 RIGHT JOIN t2 on t2.uid = t1.uid WHERE t2.date IS NOT NULL
Regards,
Eduard
Try using LEFT JOIN[^] like this:
SELECT * FROM tbl1 LEFT JOIN tbl2 ON tlb1.uid = tbl2.uid WHERE tbl2.uid = NULL
OP remarked that the result was empty and he is right the statement should be:
SELECT tbl1.uid, tbl1.name FROM tbl1 LEFT JOIN tbl2 ON tbl1.uid = tbl2.uid WHERE tbl2.uid IS NULL
I also changed theSELECT
as otherwise theNULL
columns oftbl2
are also shown.
Try as below Query.
Select * from Table1 T1 where T1.uid not in (Select T2.uid from Table2 T2);
这篇关于从MySQL数据库的两个表中获取行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文