Below are the 2 queries I need to combine into one, and I like to have rows from the left table even if there is no matching row in the right table.
$sql = "SELECT country.printable_name FROM sku"
. "LEFT OUTER JOIN country ON country.numcode=sku.c_code"
. "WHERE sku.item_sku='JDJ2020'";
$sql= "SELECT hs_code FROM hs_codes"
. "INNER JOIN sku ON
sku.base_category=hs_codes.base_category AND
sku.sub_category=hs_codes.sub_category"
. "WHERE sku.item_sku = 'JDJ2020'";
After PHP code clearing the queries will look like
SELECT country.printable_name
FROM sku
LEFT OUTER JOIN country ON country.numcode=sku.c_code
WHERE sku.item_sku='JDJ2020';
SELECT hs_code
FROM hs_codes
INNER JOIN sku ON sku.base_category=hs_codes.base_category
AND sku.sub_category=hs_codes.sub_category
WHERE sku.item_sku = 'JDJ2020';
Their combining may look like
SELECT country.printable_name, hs_codes.hs_code
FROM sku
INNER JOIN hs_codes ON sku.base_category=hs_codes.base_category
AND sku.sub_category=hs_codes.sub_category
LEFT OUTER JOIN country ON sku.c_code=country.numcode
WHERE sku.item_sku='JDJ2020';
If there exists a row in the country
which matches the row in sku
always then replace LEFT OUTER JOIN
with INNER JOIN
.
If there exists more than one matched row in country
and/or hs_codes
then you'll obtain more than one output row.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.