SELECT * FROM (
SELECT id_product, picture_url, product_name, product_price, tracking_link, '$ecommerce2->nama_ecommerce' AS table_identity FROM table1 WHERE product_name LIKE '%$search%'
UNION
SELECT id_product, picture_url, product_name, product_price, tracking_link, '$ecommerce2->nama_ecommerce' AS table_identity FROM table2 WHERE product_name LIKE '%$search%'
) AS big_table ORDER BY big_table.product_price DESC
LIMIT 1
How to get first and the last record from sql above?
只需在desc
(用于最后一条记录)和asc
(用于第一条记录)之间切换,而limit 1
仅用于一条记录。
Assuming your Union is duplicated for a reason:
SELECT * FROM (
SELECT id_product, picture_url, product_name, product_price, tracking_link, '$ecommerce2->nama_ecommerce' AS table_identity FROM table1 WHERE product_name LIKE '%$search%'
UNION ALL
SELECT id_product, picture_url, product_name, product_price, tracking_link, '$ecommerce2->nama_ecommerce' AS table_identity FROM table2 WHERE product_name LIKE '%$search%'
) AS big_table1 ORDER BY product_price LIMIT 1
UNION
SELECT * FROM (
SELECT id_product, picture_url, product_name, product_price, tracking_link, '$ecommerce2->nama_ecommerce' AS table_identity FROM table1 WHERE product_name LIKE '%$search%'
UNION ALL
SELECT id_product, picture_url, product_name, product_price, tracking_link, '$ecommerce2->nama_ecommerce' AS table_identity FROM table2 WHERE product_name LIKE '%$search%'
) AS big_table2 ORDER BY product_price DESC LIMIT 1
(SELECT * FROM (
SELECT id_product, picture_url, product_name, product_price, tracking_link, '$ecommerce2->nama_ecommerce' AS table_identity FROM table1 WHERE product_name LIKE '%$search%'
UNION ALL
SELECT id_product, picture_url, product_name, product_price, tracking_link, '$ecommerce2->nama_ecommerce' AS table_identity FROM table2 WHERE product_name LIKE '%$search%'
) AS big_table1 ORDER BY product_price LIMIT 1)
UNION
(SELECT * FROM (
SELECT id_product, picture_url, product_name, product_price, tracking_link, '$ecommerce2->nama_ecommerce' AS table_identity FROM table1 WHERE product_name LIKE '%$search%'
UNION ALL
SELECT id_product, picture_url, product_name, product_price, tracking_link, '$ecommerce2->nama_ecommerce' AS table_identity FROM table2 WHERE product_name LIKE '%$search%'
) AS big_table2 ORDER BY product_price DESC LIMIT 1)
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.