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

时间:2015-05-15 08:12:55

标签: mysql sql mysql-python

我有以下表格:

'价格'

+------------+--------------+------+-----+---------+----------------+
| 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    |                |
+------------+--------------+------+-----+---------+----------------+

'产品'

+-----------------+--------------+------+-----+---------+----------------+
| 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之间。

我正在运行一个脚本来抓取网址,从特定网页上抓取价格,然后每小时更新一次“价格”表。

我想显示以下内容,理想情况下是一个数据库查询:

  • products.product_title
  • products.product_image
  • prices.price(当前价格,即每个产品ID的最后一个插页)
  • 今天的平均价格与昨天的平均价格之间的百分比差异
  • 当月的平均价格与上个月的平均价格之间的百分比差异

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

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

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

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

2 个答案:

答案 0 :(得分:1)

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

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;

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

答案 1 :(得分:0)

根据@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;

我不确定如何计算查询中的百分比差异,但我会用Python来保持查询的清洁。