一个查询中具有不同条件的多个 SELECT 语句

Multiple SELECT statements with different conditions in one query

我有以下 tables:

'Prices'

+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| product_id | int(11)      | YES  |     | NULL    |                |
| price      | text         | YES  |     | NULL    |                |
| date       | text         | YES  |     | NULL    |                |
| time       | text         | YES  |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+

'Products'

+-----------------+--------------+------+-----+---------+----------------+
| Field           | Type         | Null | Key | Default | Extra          |
+-----------------+--------------+------+-----+---------+----------------+
| id              | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| category_id     | int(11)      | YES  |     | NULL    |                |
| product_urls    | text         | YES  |     | NULL    |                |
| product_title   | text         | YES  |     | NULL    |                |
| product_image   | text         | YES  |     | NULL    |                |
| product_content | text         | YES  |     | NULL    |                |
+-----------------+--------------+------+-----+---------+----------------+

这里的连接在Products.idPrices.products_id之间。

我有一个脚本 运行 可以抓取 URL,从特定网页抓取价格,然后每小时更新 'Prices' table。

我想显示以下内容,最好使用一个数据库查询:

我可以单独做这些事情,但我不能把它们合并到一个查询中。

为了清楚起见,我想一次获取特定类别中的所有产品 - 考虑到这一点,我一直在这样做:

...WHERE category_id=%s...GROUP BY product_id...

我正在直接编写 SQL,特别是使用 MySQLdb Python 库。

第一个子选择的未测试示例(在此模板上进行其他子选择):

SELECT p.*,
     (SELECT prices.price
          FROM prices
          WHERE prices.product_id = p.id
          ORDER BY prices.id DESC
          LIMIT 1) as last_price
FROM product p
WHERE p.category_id = 4;

可能不是最有效的方法...

根据@Cosmin 的建议的最终片段...

SELECT p.*,
     (SELECT prices.price
          FROM prices
          WHERE prices.product_id = p.id
          ORDER BY prices.id DESC
          LIMIT 1) as last_price,

     (SELECT ROUND(AVG(prices.price), 2)
          FROM prices
          WHERE prices.product_id = p.id AND date = CURRENT_DATE()
          ORDER BY prices.id DESC
          LIMIT 1) as todays_average_price,

     (SELECT ROUND(AVG(prices.price), 2)
          FROM prices
          WHERE prices.product_id = p.id AND date = SUBDATE(CURRENT_DATE, 1)
          ORDER BY prices.id DESC
          LIMIT 1) as yesterdays_average_price,

     (SELECT ROUND(AVG(prices.price), 2)
          FROM prices
          WHERE prices.product_id=p.id AND MONTH(date)=MONTH(current_date)
          ORDER BY prices.id DESC
          LIMIT 1) as current_months_average_price,

     (SELECT ROUND(AVG(prices.price), 2)
          FROM prices
          WHERE prices.product_id=p.id AND MONTH(date)=MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
          ORDER BY prices.id DESC
          LIMIT 1) as previous_months_average_price
FROM products p
WHERE p.category_id=%s;

我不是 100% 确定如何在查询中计算百分比差异,但我将使用 Python 来计算以保持查询清晰。