SQL - JOIN和SUB QUERY

时间:2014-02-28 03:10:10

标签: sql

我想加入两张表

Table:  SignUp                                 Table: Thinker
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
User_Id--+--Printer_Model--+--Product_No--+--Thinker_Id--+--Printer_Model--+--Product_No
1           HP OfficeJet 6480 C6615DN;CPT    8363           HP OfficeJet 6400 C6615DN;CPT
2           HP PSC 950        CB335WN;HEW    9436           HP PSC 950        CB335WN;HEW

并且能够接收客户尚未选择但是打印机需要操作员的类似产品。例如,客户搜索HP15Black,他们将需要HP78 Tri-Color,它将位于其网页的左侧。所以我试图使用JOIN和Sub Query来!=或<>他们已经拥有但没有找到%LIKE%的Product_No。下面的脚本将显示相同的产品,而不是类似的产品。帮助...... Jason Kraft - InksEtc http://inks-etc.com

SELECT s.`User_Id`,s.`Username`,s.`Printer_Model`, s.`Product_No`,t.`Thinker_Id`,t.`Printer_Model`, t.`Product_No`
    FROM `Thinker2014` t
    RIGHT JOIN
(
    SELECT `User_Id`,`Brand`,`Username`,`Printer_Model`,`Product_No`
    FROM `SignUp`
    WHERE  `Brand` =
              (SELECT `Brand` FROM `SignUp` WHERE `User_Id`='1')
    AND  `Product_No` =
              (SELECT `Product_No` FROM `SignUp` WHERE `User_Id`='1')
    AND    `Printer_Model` =
              (SELECT `Printer_Model` FROM `SignUp` WHERE `User_Id`='1')
    ) AS s
    ON s.`Brand`=t.`Brand`
    GROUP BY s.Brand
    LIMIT 25

0 个答案:

没有答案