需要使用不同的WHERE找到一个结果。 SELECT中的SELECT?

时间:2013-12-12 17:17:00

标签: mysql sql

我有这个问题:

SELECT a.WEEK_ID, a.QUANTITY, a.PRICE AS expenseTotal, 
b.ID, b.COMMENCE, b.USER_ID,b.*, 
c.ID AS userid, c.NAME, c.LAST_NAME 
from b_sale_basket a 
INNER JOIN b_report_week b ON a.WEEK_ID = b.ID 
INNER JOIN b_user c ON b.USER_ID = c.ID 
WHERE a.ORDER_ID = $ID AND a.PRODUCT_ID = 316

您将在第一行看到此部分:

a.PRICE AS expenseTotal

我想要找到一个.PRICE a.PRODUCT_ID = 317不是316当前。

我试图在SELECT中执行SELECT语句:

  SELECT a.WEEK_ID, a.QUANTITY, 
 (SELECT PRICE FROM b_sale_basket WHERE PRODUCT_ID = 317 AND ORDER_ID = $ID) AS expenseTotal, 
  b.ID, b.COMMENCE, b.USER_ID,b.*, 
  c.ID AS userid, c.NAME, c.LAST_NAME from b_sale_basket a 
  INNER JOIN b_report_week b ON a.WEEK_ID = b.ID 
  INNER JOIN b_user c ON b.USER_ID = c.ID 
  WHERE a.ORDER_ID = $ID AND a.PRODUCT_ID = 316

但那没用。请有人指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

您需要添加另一个JOIN

类似的东西:

SELECT a.WEEK_ID, a.QUANTITY, 
 d.PRICE AS expenseTotal, 
  b.ID, b.COMMENCE, b.USER_ID,b.*, 
  c.ID AS userid, c.NAME, c.LAST_NAME 
  FROM b_sale_basket a 
  INNER JOIN b_report_week b ON a.WEEK_ID = b.ID 
  INNER JOIN b_user c ON b.USER_ID = c.ID 
  INNER JOIN b_sales_basket d ON d.ORDER_ID = a.ORDER_ID AND d.PRODUCT_ID = 317
  WHERE a.ORDER_ID = $ID AND a.PRODUCT_ID = 316