I want to join the last inserted comma separated values from daily_discounts.discount_product table limit 1 in the products table. Its possible to achieve this in one query ?
Tried :
SELECT * FROM `daily_discounts` INNER JOIN products WHERE FIND_IN_SET(products.product_id , daily_discounts.discount_products)
SELECT * FROM `daily_discounts` INNER JOIN products ON FIND_IN_SET(products.product_id , daily_discounts.discount_products) LIMIT 1
Table daily_discounts:
+-------------+---------------------+
| discount_id | discount_products |
+-------------+---------------------+
| 1 | 960,310,165,702 |
+-------------+---------------------+
| 2 | 231,822,379,420 |
+-------------+---------------------+
| 3 | 518,56,803,858 |
+-------------+---------------------+
Table products result wanted :
+------------+--------------------+
| product_id | product_sale_price |
+------------+--------------------+
| 518 | 10.25 |
+------------+--------------------+
| 56 | 11.24 |
+------------+--------------------+
| 803 | 5.55 |
+------------+--------------------+
| 858 | 13.52 |
+------------+--------------------+
I'm using two queries in order to work First I select the last inserted record on daily_discounts table:
select `discount_products` from `daily_discounts` order by `discount_id` desc limit 1
Second I Select the Products table
select `product_id`, `product_sale_price from `products` where `product_id` in (518, 56, 803, 858) order by `product_id` desc)