Saturday 21 September 2019

subquery with multiple columns value comparison

REF:

Other Uses of Subqueries

The SQL Standard, effective with SQL:1999, requires increased subquery support, which MySQL provides. The row subqueries alluded to earlier are an example. Thus, it is now possible to compare multiple columns at a time:
SELECT ROW ('smith', 'auditor') =
   (SELECT lname, job FROM clients WHERE clno = 10);
The subquery in this example returns a row containing the values 'smith' and 'auditor'. When these values are compared to the ROW values in the outer query, they are found to be equal and so the query returns 1 (true).

http://download.nust.na/pub6/mysql/tech-resources/articles/4.1/subqueries.html

Example:

SELECT p.ID, p.entity_ID entity_ID, p.product_type, p.product_name, p.product_keyword, p.listing_desc,p.product_desc,p.industry_type,p.product_subcategory, p.brand_name,p.contact_country,p.industry_type, p.product_subcategory, p.order_unit, p.order_qty_max, p.order_qty_min, p.pref_selling_price, p.country, p.state,p.product_category,p.Shipping_from, p.selling_price_max as maxSellingPrice, p.selling_price_min as minSellingPrice,

ROW ('pref_selling_price', 'order_unit') = (SELECT order_qty_price, pp.order_qty_unit FROM product_price_detail as pp WHERE pp.products_id = 'Prod5d84a4f8af065' LIMIT 1) as compcolum,

AVG(r.rating) as productRating,c.company_name as Title, c.about_company as Description, c.contact_email as Email, c.ID as sellerID, c.company_city, c.countryid FROM product as p LEFT JOIN product_rating as r ON p.ID = r.product_id LEFT JOIN company as c ON p.entity_ID = c.entity_ID LEFT JOIN product_price_detail as ppt ON ppt.products_id = p.ID WHERE p.ID = '[...]


compcolum : Return 0 if not matches or return 1 if matches

No comments:

Post a Comment