I have a SELECT query from two different tables (Product & Supplier Codes - inner joined) where a specified column is %LIKE% my search string.
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. 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).
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?
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.
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. 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, 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
LIKE
is for the value, not the column, ie.Barcode LIKE '%4321%'
orAlt_Barcode LIKE '%4321%'
– Sean