mysql - Multiple SELECT statements with different conditions in one query -


i have following tables:

'prices'

+------------+--------------+------+-----+---------+----------------+ | field      | type         | null | key | default |          | +------------+--------------+------+-----+---------+----------------+ | 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 |          | +-----------------+--------------+------+-----+---------+----------------+ | 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    |                | +-----------------+--------------+------+-----+---------+----------------+ 

the connection here between products.id , prices.products_id.

i have script running grabs urls, scrapes prices particular web pages , updates 'prices' table every hour.

i'd display following, ideally 1 database query:

  • products.product_title
  • products.product_image
  • prices.price (current price, i.e. last insert each product id)
  • the percentage difference between today's average price , yesterday's average price
  • the percentage difference between current month's average price , previous month's average price

i can these things separately, cannot bring 1 query.

just clarity, wanting grab of products @ once within particular category - in mind have been doing this:

...where category_id=%s...group product_id... 

i writing sql directly, using mysqldb python library.

untested example first subselect (make other subselects on template) :

select p.*,      (select prices.price           prices           prices.product_id = p.id           order prices.id desc           limit 1) last_price product p p.category_id = 4; 

probably not efficient way tough...


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -