dsaj20411 2012-02-06 15:30
浏览 103
已采纳

使用JOIN匹配第一个结果

I'm trying to obtain the price from a field called DiscountMarkupPriceRate1 on my second table to display in PHP.

The first portion of my query pulls what I need correctly, the Parentsku of all visible products with inventory. ie GTR101.

I'm trying to join it with a second table and retrieve only the first DiscountMarkupPriceRate1 for the parent (GTR101%) where ItemCustomerPriceLevel is M.

Here's what my table looks like. This is essentially the result of the first half of my query before the join (stripped of all the other fields I need):

**INVENTORY**
SKU      store_quantity     parent_sku  visible
----------------------------------------------------------------
GTR101      20      NULL        Y
GTR102      100     NULL        Y
GTR103      88      NULL        Y

This is the second table:

**ins_imb_1**

DiscountMarkupPriceRate1    ItemNumber   ItemCustomerPriceLevel           
-----------------------------------------------------------------
15.950             GTR101S          M
15.950             GTR101M          M
11.950             GTR101L          M
10.000             GTR101S          T

I'm trying to get

GTR101   15.95

and here's what I have for a query:

Select * 
from INVENTORY
where parent_sku='' 
AND store_quantity > 0 
AND SKU like '%GTR%' 
AND visible='Y'

LEFT JOIN ins_imb_1
ON ins_imb_1.ItemNumber =
    (
    SELECT ItemNumber, ItemCustomerPriceLevel, DiscountMarkupPriceRate1
    FROM ins_imb_1
    WHERE ins_imb_1.ItemNumber% = INVENTORY.SKU 
    AND ins_imb_1.ItemCustomerPriceLevel = 'M'
    ORDER BY 
            INVENTORY.SKU
    LIMIT 1
    )
  • 写回答

1条回答 默认 最新

  • duanpu5048 2012-02-06 15:41
    关注

    First thing wrong I see here, is that JOINs need to be after the FROM statement and before WHERE.

    Also your subquery in the LEFT JOIN is wrong.

    LEFT JOIN ins_imb_1
    ON ins_imb_1.ItemNumber =
    (
      SELECT ItemNumber, ItemCustomerPriceLevel, DiscountMarkupPriceRate1
    

    Your subquery should only return one field (for it to compare to ins_imb_1.ItemNumber).

    I don't know if you even need a subquery here, you could do something like this:

    LEFT JOIN ins_imb_1
    ON ins_imb_1.ItemNumber LIKE CONCAT(INVENTORY.SKU, '%')
    AND ins_imb_1.ItemCustomerPriceLevel = 'M'
    

    I also see few things that could be optimized here.

    where parent_sku='' should be where parent_sku IS NULL.

    AND SKU like '%GTR%' should be AND SKU like 'GTR%'(as the SKU always start with 'GTR').

    SELECT *
    FROM INVENTORY
    
    LEFT JOIN ins_imb_1
    ON ins_imb_1.ItemNumber LIKE CONCAT(INVENTORY.SKU, '%')
    AND ins_imb_1.ItemCustomerPriceLevel = 'M'
    
    WHERE parent_sku IS NULL
    AND store_quantity > 0
    AND SKU LIKE 'GTR%'
    AND visible = 'Y'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)