简体   繁体   English

将两个表中的列合并为一个结果,然后在SELECT查询中使用

[英]Combine columns from two tables AS one result and THEN use in SELECT query

I have a SELECT query from two different tables ( Product & Supplier Codes - inner joined) where a specified column is %LIKE% my search string. 我从两个不同的表( 产品供应商代码 -内部联接)中有一个SELECT查询,其中指定的列是我的搜索字符串%LIKE%。

SELECT product.*, supplier_code.PART_NO AS PART_NO
FROM product INNER JOIN supplier_code ON 
product.PRODUCT_ID=supplier_code.PRODUCT_ID  
AND supplier_code.MAINSUPP = 1 
WHERE $column LIKE ? 
LIMIT 0, 25

The problem I'm having is one of the columns ( Barcode ) has alternatives which are stored in a third table - AltBCode . 我遇到的问题是列( Barcode )之一,其中的替代项存储在第三个AltBCode中 So if $column = Barcode, that match could be in Product.Barcode OR it could be in AltBCode.Alt_Barcode (never both and both are unique). 因此,如果$ column = Barcode,则该匹配项可能在Product.Barcode中,或者可能在AltBCode.Alt_Barcode中(两者都不是唯一的)。

I can only asume I need to combine these two columns together first as one AllBarcodes column say, and then use this in my Select query? 我只能假设我需要首先将这两个列组合在一起,就像一个AllBarcodes列所说的那样,然后在我的Select查询中使用它?

The layout of the two barcode storing tables is: 两个条形码存储表的布局为:

Product Table
PRODUCT_ID | Barcode | Description | Price | Stock | Etc
1          |  12345  |   Apple     | 1.00  |    4  |  
2          |  45678  |   Orange    | 0.50  |    2  |
3          |  91583  |   Banana    | 2.00  |    0  |

AltBCode Table
Id | PRODUCT_ID | Alt_Barcode
1  |    2       |   4321

So if I search for "4321" I want to return Row 2 from Product. 因此,如果我搜索“ 4321”,我想从产品返回第2行。

UPDATE 更新

Thanks for the responses guys. 谢谢你们的回应。 I'm not sure I was clear enough though. 我不确定自己是否足够清楚。

Once Product.Barcode and AltBCODE.Alt_code are joined they are the $column I want to be able to use in my WHERE clause. 一旦将Product.BarcodeAltBCODE.Alt_code连接在一起 ,它们就是我希望能够在WHERE子句中使用的$ column。 So: 所以:

SELECT product.*, supplier_code.PART_NO AS PART_NO
(Magically Select product.barcode and altbcode.altcode AS ALLBARCODES)
FROM product INNER JOIN supplier_code ON 
product.PRODUCT_ID=supplier_code.PRODUCT_ID  
AND supplier_code.MAINSUPP = 1 
WHERE ALLBARCODES LIKE %4321%
LIMIT 0, 25

So to simplify if: 因此,如果简化:

Table Product
PROD_ID | Barcode | Desc
2       |  1234   | Apples

Table Alt Barcodes'
ID | PROD_ID | ALT_CODE
 1 |    2    | 2345'
 2 |    2    | 3456
 3 |    2    | 4567

WHERE PROD_ID = 2 would give me 4 rows: PROD_ID = 2会给我4行:

PROD_ID -> 2, ALLBARCODE ->1234, DESC- > APPLES  
PROD_ID -> 2, ALLBARCODE ->2345, DESC- > APPLES  
PROD_ID -> 2, ALLBARCODE ->3456, DESC- > APPLES  
PROD_ID -> 2, ALLBARCODE ->4567, DESC- > APPLES  

BUT WHERE ALLBARCODE LIKE %2345% would only match 1 但是像ALLBARCODE这样的%2345%只能匹配1

First left join Product Table and AltBCode Table so you have both code on same table. 首先left join Product TableAltBCode Table以便您在同一表上都具有代码。

SELECT PT.*, AT.Alt_Barcode
FROM `Product Table` PT
LEFT JOIN `AltBCode Table`  AT
       ON PT.PRODUCT_ID = AT.PRODUCT_ID 

Your Final query should be 您的最终查询应为

I use = instead of LIKE 我用=代替LIKE

SELECT *,
       $column as MatchBarcode
FROM supplier S
JOIN `Product Table` PT
  ON S.PRODUCT_ID = PT.PRODUCT_ID 
LEFT JOIN `AltBCode Table`  AT
       ON PT.PRODUCT_ID = AT.PRODUCT_ID 
WHERE S.MAINSUPP = 1 
  AND ( 
          $column = AT.`Alt_Barcode`
       OR $column = PT.`Barcode`
      )   

First I tought in use 首先我在使用

CASE 
    WHEN $column = AT.`Alt_Barcode` THEN AT.`Alt_Barcode`
    WHEN $column = PT.`Barcode` THEN PT.`Barcode`
END as MatchBarcode        

But that is equivalent to $column as MatchBarcode 但这相当于$column as MatchBarcode

If I'm understanding the issue correctly, you can use a CASE WHEN to do this. 如果我正确理解了该问题,则可以使用CASE WHEN来执行此操作。 Join to the AltBCode table first, then use the CASE statement to filter which column you return based on the criteria (might need to change the conditions). 首先连接到AltBCode表,然后使用CASE语句根据条件过滤返回的列(可能需要更改条件)。 Something like: 就像是:

SELECT product.*, supplier_code.PART_NO AS PART_NO,
    case 
        when MyColumn = 'Barcode' then Product.Barcode
        when MyColumn = 'NotBarcode' then AltBCode.Alt_Barcode
    end as 'Barcode'
FROM product INNER JOIN supplier_code ON 
product.PRODUCT_ID=supplier_code.PRODUCT_ID  
AND supplier_code.MAINSUPP = 1 
inner join AltBCode on product.PRODUCT_ID = AltBCode.PRODUCT_ID
WHERE $column LIKE ?
LIMIT 0, 25

Hope that helps 希望能有所帮助

Do that: 去做:

   SELECT
  a.* from product a, altbProduct b
  where a.product_id = b.product_id
and (a.barcode='4321' or b.altbarcode='4321');

Here is a simple SQLFidle without the supplier_code: DEMO 这是一个不带Supplier_code的简单SQLFidle: DEMO

you want to left join the alt table to not filter out rows and use coalesce 您想离开联接表以不滤除行并使用合并

SELECT product.*, supplier_code.PART_NO AS PART_NO, coalesce(at. Alt_Barcode, supplier_code.barcode) as barcode
FROM product 
LEFT JOIN supplier_code ON product.PRODUCT_ID=supplier_code.PRODUCT_ID AND supplier_code.MAINSUPP = 1 
LEFT JOIN AltBCode ac ON product.PRODUCT_ID = ac.PRODUCT_ID
WHERE $column LIKE ? 
LIMIT 0, 25

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM