我有一个简单的查询
SELECT AGENT FROM `SALES` WHERE PRICE='3000.00' AND COMDTE > '2014-02-02' AND COMDTE < '2014-03-02'
我还有第二张桌子,我有代理人的参数RANK
我需要选择代理商有RANK ='1'
的所有销售但是还有2张桌子。销售和代理以及代理表中的参数RANK
SELECT ID FROM `AGENTS` WHERE RANK = 1
我的尝试是
SELECT AGENT FROM `SALES` WHERE PRICE='3000.00' AND COMDTE > '2014-02-02' AND COMDTE < '2014-03-02' IF ((SELECT ID FROM `AGENTS` WHERE RANK = 1 AND ID = AGENT),YES, NO) = YES
答案 0 :(得分:1)
您需要一个简单的连接
SELECT s.AGENT
FROM `SALES` s
JOIN `AGENTS` a ON(s.AGENT =a.id)
WHERE s.PRICE='3000.00'
AND s.COMDTE > '2014-02-02'
AND s.COMDTE < '2014-03-02'
AND a.RANK = 1
答案 1 :(得分:0)
SELECT AGENT
FROM `SALES`
WHERE PRICE='3000.00'
AND COMDTE > '2014-02-02'
AND COMDTE < '2014-03-02'
AND AGENT IN (SELECT ID FROM `AGENTS` WHERE RANK = 1)
答案 2 :(得分:-1)
那不会起作用,你必须创建一个名为view table的东西,它是一个新表,它组合了2个表,然后从中检索数据
在此处了解http://www.w3resource.com/sql/creating-views/creating-view.php