如何最好地加入这两个查询

时间:2013-10-15 10:09:24

标签: mysql

我有以下2个查询,并希望在store_id上​​加入它们,以获得在特定邮政编码区域销售戴尔产品的商店数量,商店是否开放,此商店的评论总数以及评论的平均评分

SELECT s.* , sh . * , d . * , pd . *, 
CASE
  When (holiday = "Y") THEN "Holiday"
END AS open_status
FROM delivery d, products_description pd, store s, store_hours sh 
WHERE d.deliver_to_postcode =5140 
AND d.store_id = pd.store_id 
AND (pd.products_name like '%dell%' or pd.products_description like '%dell%') 
AND pd.store_id = s.store_id 
AND s.store_id = sh.store_id 
AND sh.weekday = dayname( curdate( ) ) 
GROUP BY  d.store_id'


'SELECT store_id, count( * ) AS  reviews, avg( reviews_rating ) AS  rating 
FROM reviews
WHERE  reviews_status =1'


Total stores  | store_id  | store_name  | Postcode | reviews | rating      | open_status
----------------------------------------------------------------------------------------------------------------------
2             |   1       | store 1     | 5140     |   3     |   2 star    | Holiday
2             |   2       | store 2     | 5140     |   1     |   2 star    | Holiday

如何加入这两个查询以提供上述结果?

我添加了数据库表

Delivery

Store_id |   postcode
--------------------------
1        |  5140
1        |  5200
2        |  5140


Product description

Store_id | product_name
------------------------
1        | pc dell
1        | pc ibm
2        | screen dell


Store

Store_id | Name
-------------------
1        | Store 1
2        | Store 2


Store hours

Store_id | Holiday
-------------------
1        | N
2        | N


Reviews

Store_id | Product  | Review    | review rating
------------------------------------------------
1        | 1        | review 1  |   3 star
1        | 1        | review 2  |   3 star
1        | 2        | review 1  |   3 star
2        | 1        | review 1  |   2 star

我添加了DB SQL

-

- 表delivery

的表结构
  CREATE TABLE IF NOT EXISTS `delivery` (
   `store_id` int(11) NOT NULL DEFAULT '1',
   `deliver_to_postcode` int(11) NOT NULL,
   PRIMARY KEY (`store_id`,`deliver_to_postcode`)
   ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-

- 表products_description

的表结构
CREATE TABLE IF NOT EXISTS `products_description` (
    `store_id` int(11) NOT NULL DEFAULT '1',
    `products_id` int(11) NOT NULL AUTO_INCREMENT,
    `language_id` int(11) NOT NULL DEFAULT '1',
    `products_name` varchar(255) NOT NULL DEFAULT '',
    `products_short_description` text,
    `products_description` text,
    `products_keyword` varchar(64) DEFAULT NULL,
    `products_tags` varchar(255) DEFAULT NULL,
    `products_url` varchar(255) DEFAULT NULL,
    `products_friendly_url` varchar(255) DEFAULT NULL,
    `products_page_title` varchar(255) NOT NULL,
    `products_meta_keywords` varchar(255) NOT NULL,
    `products_meta_description` varchar(255) NOT NULL,
    `products_viewed` int(5) DEFAULT '0',
     PRIMARY KEY (`store_id`,`products_id`,`language_id`),
     KEY `products_name` (`products_name`),
     KEY `products_description_keyword` (`products_keyword`)
  ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=20 ;

-

- 表store

的表结构
CREATE TABLE IF NOT EXISTS `store` (
   `store_id` int(11) NOT NULL AUTO_INCREMENT,
   `customer_id` int(11) NOT NULL DEFAULT '1',
   `store_name` varchar(64) NOT NULL,
   PRIMARY KEY (`store_id`),
   KEY `customer_id` (`customer_id`)
 ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

-

- 表store_hours

的表结构
CREATE TABLE IF NOT EXISTS `store_hours` (
    `store_id` int(11) NOT NULL,
    `weekday` varchar(10) NOT NULL,
    `holiday` char(1) NOT NULL DEFAULT 'N',
    KEY `store_id` (`store_id`)
 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-

- 表reviews

的表结构
 CREATE TABLE IF NOT EXISTS `reviews` (
    `store_id` int(11) NOT NULL DEFAULT '1',
    `reviews_id` int(11) NOT NULL AUTO_INCREMENT,
    `products_id` int(11) NOT NULL,
    `customers_id` int(11) DEFAULT NULL,
    `reviews_rating` int(1) DEFAULT NULL,
    `languages_id` int(11) NOT NULL,
    `reviews_text` text NOT NULL,
    `date_added` datetime DEFAULT NULL,
    `last_modified` datetime DEFAULT NULL,
    `reviews_read` int(5) NOT NULL DEFAULT '0',
    `reviews_status` tinyint(1) NOT NULL,
     PRIMARY KEY (`store_id`,`reviews_id`)
  ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

3 个答案:

答案 0 :(得分:0)

试试这个

SELECT
    count(s.store_id) count,
    s.*,
    sh.*,
    ( SELECT count(*)
      FROM reviews r
      WHERE r.reviews_status = 1 AND r.store_id = s.store_dd ) reviews
FROM store s
JOIN delivery d ON s.store_id = d.store_id
JOIN products_description pd ON pd.store_id = d.store_id
JOIN store_hours sh ON sh.store_id = d.store_id
WHERE
    d.deliver_to_postcode = 5140 AND
    sh.weekday = dayname(curdate()) AND
    (pd.products_name LIKE '%dell%' OR pd.products_description LIKE '%dell%')
GROUP BY s.store_id

我不能为你做更多,因为我没有你的数据库模型/结构

答案 1 :(得分:0)

回答部分问题 - 这是加入两个查询时要遵循的语法格式 - 在 [[查询占位符]] 中填写SQL语句

SELECT  *
FROM (
  [[query1]]
  ) AS q1
JOIN (
   [[query2]]
  ) AS q2
  ON q1.store_id = q2.store_id

注意:我使用星号是出于演示目的,请不要将其投入生产。另外,我不明白为什么你的第一个查询使用GROUP BY - 没有聚合函数。

答案 2 :(得分:0)

您可以在此处查看解决方案:http://sqlfiddle.com/#!2/691a1/8

这个解决方案有一些注意事项:

  • 我相信您上面的查询是"错误"。第一个查询具有GROUP BY,而不是聚合函数。您的第二个查询具有聚合函数,但没有GROUP BY行。我将GROUP BY子句移到了第二个查询。
  • 使用星号是过度的。 SQLFiddle 列出重复的列。因此,store_id只列出一次。
  • 我将第一个查询从使用隐式(theta style)连接更改为显式(ANSI样式)连接。

这是查询

SELECT tmp.*, s.* , sh.* , d.* , pd.*, 
  CASE
    When (holiday = "Y") THEN "Holiday"
  END AS open_status
FROM delivery d
JOIN  products_description pd
  ON d.store_id = pd.store_id
JOIN store s
  ON pd.store_id = s.store_id
JOIN store_hours sh 
  ON s.store_id = sh.store_id
JOIN (
    SELECT store_id, count(*) AS  reviews, avg(reviews_rating) AS  rating 
    FROM reviews
    WHERE  reviews_status =1
    GROUP BY store_id) AS tmp
  ON tmp.store_id = s.store_id
WHERE d.deliver_to_postcode =5140 
  AND (pd.products_name like '%dell%' or pd.products_description like '%dell%') 
  AND sh.weekday = dayname( curdate( ) )