[英]Return an entry from a table based on criteria, which is not in another table
I need help with a query and i cant figure out how to make it work. 我需要有关查询的帮助,我无法弄清楚如何使其工作。
Table1 表格1
uid | G | L
------------
cde 2 1
fgk 1 2
kgl 2 1
Table2 表2
uid1 |uid2
-----------
abc cde
fgk cde
mnm kgl
I have a known uid which is 我有一个已知的uid
uid | G | L
-----------
abc 1 2
and i must match this uid with one from Table1 而且我必须将此uid与Table1中的一个匹配
My query for this is : 我对此的查询是:
SELECT * FROM Table1 WHERE G=2 AND L=1 ORDER BY RAND() LIMIT 1
This will return: 这将返回:
cde 2 1
kgl 2 1
The query that i am looking for must return only kgl
because cde
is already paired with abc
in Table2 我要查找的查询必须仅返回kgl
因为cde
已在Table2中与abc
配对
Any ideas? 有任何想法吗?
UPDATE : With some tweaking i have come up with this query: 更新 :通过一些调整,我想出了以下查询:
SELECT uid FROM table1 AS t1
WHERE G = 1 AND L = 2 AND NOT EXISTS
(SELECT * FROM table2 AS t2
WHERE (t1.uid = t2.uid1 OR t1.uid=t2.uid2) AND (t2.uid1 = 'abc' OR t2.uid2 = 'abc'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.