我有以下 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
我已经添加了数据库 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 ;