Mysql 如何在单个选择查询中进行多个选择查询。
我必须在哪里写一个查询,我需要获取上周、上个月以及所有的记录。 对于这个问题,我为上周、上个月和所有人写了3个不同的查询 有关每周信息:-Mysql 如何在单个选择查询中进行多个选择查询。,mysql,sql,Mysql,Sql,我必须在哪里写一个查询,我需要获取上周、上个月以及所有的记录。 对于这个问题,我为上周、上个月和所有人写了3个不同的查询 有关每周信息:- SELECT bu.brand_name AS 'Brand_Name',COUNT(s.unique) AS '# Item Sold',SUM(s.price) AS 'Total_Price' FROM item_details s LEFT JOIN sales_order o ON s.fk_sales_order = o.id_sales_ord
SELECT bu.brand_name AS 'Brand_Name',COUNT(s.unique) AS '# Item Sold',SUM(s.price) AS 'Total_Price'
FROM item_details s
LEFT JOIN sales_order o ON s.fk_sales_order = o.id_sales_order
LEFT JOIN customer_info AS c ON o.fk_customer_id = c.id_customer
LEFT JOIN simple_details cc ON s.unique = cc.unique
LEFT JOIN config_details cf ON cc.fk_config_id = cf.config_id
LEFT JOIN brand_details cb ON cf.fk_brand_id = cb.brand_id
LEFT JOIN category_details ctc ON cf.fk_category_id = ctc.category_id
LEFT JOIN gender_details g ON cf.fk_gender_id = g.gender_id
LEFT JOIN buyers AS bu ON bu.brand_name = cb.name AND bu.category_name = ctc.name AND bu.gender = g.name
WHERE bu.buyers = 'xyz' AND DATE_FORMAT(o.created_date,'%Y-%m-%d') >= @weekstartdate AND DATE_FORMAT(o.created_date,'%Y-%m-%d') <= @weekenddate
GROUP BY bu.brand_name
这些都能很好地提供电流输出。
但问题是,我必须将这三个查询合并到一个查询中。
其中输出应为
品牌名称、项目soldweek、总价Week、项目soldmonth、总价Month、项目soldall、总价All
如何编写此查询?如果所有三个选择在结果中使用相同的字段,则可以合并它们:
SELECT *
FROM (SELECT 1) AS a
UNION (SELECT 2) AS b
UNION (SELECT 3) AS c
如果需要相互区分周/月/所有记录-只需添加包含周或月的常量字段即可如果所有三个选项在结果中使用相同字段,则可以合并它们:
SELECT *
FROM (SELECT 1) AS a
UNION (SELECT 2) AS b
UNION (SELECT 3) AS c
如果您需要相互区分week/mon/所有记录,只需添加包含week或mon的常量字段即可。您可以在查询之间使用UNION.KEYONE将它们捆绑在一起。但所有查询中的tje列类型和顺序必须相同。您可以向每个集合添加一个标识符您可以在查询之间使用UNION.keyword将它们捆绑在一起,但所有查询中的tje列类型和顺序必须相同。您可以在不深入查看代码的情况下为每个集合添加标识符,显而易见的解决方案是
SELECT
all.brand_name
pw.items_sold items_sold_week
pw.total_price total_price_week
pm.items_sold items_sold_month
pm.total_price total_price_month
all.items_sold items_sold_all
all.total_price total_price_all
FROM
(your all-time select) all
JOIN (your per-month select) pm ON all.brand_name = pm.brand_name
JOIN (your per-week select) pw ON all.brand_name = pw.brand_name
尽管您可能应该重新考虑您的整个方法,并确定您是真的希望在DB层中使用这种逻辑,还是最好在应用程序中使用这种逻辑。在不深入研究代码的情况下,显而易见的解决方案是
SELECT
all.brand_name
pw.items_sold items_sold_week
pw.total_price total_price_week
pm.items_sold items_sold_month
pm.total_price total_price_month
all.items_sold items_sold_all
all.total_price total_price_all
FROM
(your all-time select) all
JOIN (your per-month select) pm ON all.brand_name = pm.brand_name
JOIN (your per-week select) pw ON all.brand_name = pw.brand_name
尽管您可能应该重新考虑您的整个方法,并确定您是真的希望在DB层中使用这种逻辑,还是最好在应用程序中使用这种逻辑。您可以使用case将聚合限制为行的子集:
select bu.brand_name
, count(case when date_format(o.created_date,'%Y-%m-%d') >= @weekstartdate
and date_format(o.created_date,'%Y-%m-%d') <= @weekenddate
then 1 end) as '# Item Sold Week'
, sum(case when date_format(o.created_date,'%Y-%m-%d') >= @weekstartdate
and date_format(o.created_date,'%Y-%m-%d') <= @weekenddate
then s.price end) as 'Total_Price Week'
, count(case when date_format(o.created_date,'%Y-%m-%d') >= @monthstartdate
and date_format(o.created_date,'%Y-%m-%d') <= @monthstartdate
then 1 end) as '# Item Sold Month'
, ...
您可以使用case将聚合限制为行的子集:
select bu.brand_name
, count(case when date_format(o.created_date,'%Y-%m-%d') >= @weekstartdate
and date_format(o.created_date,'%Y-%m-%d') <= @weekenddate
then 1 end) as '# Item Sold Week'
, sum(case when date_format(o.created_date,'%Y-%m-%d') >= @weekstartdate
and date_format(o.created_date,'%Y-%m-%d') <= @weekenddate
then s.price end) as 'Total_Price Week'
, count(case when date_format(o.created_date,'%Y-%m-%d') >= @monthstartdate
and date_format(o.created_date,'%Y-%m-%d') <= @monthstartdate
then 1 end) as '# Item Sold Month'
, ...
谢谢,它在“of item salled Week”和“of item salled Month”中工作正常,但在“Total_Price Week”和“Total_Price Month”中显示为空,这很奇怪,您确定要使用s.Price end表示价格,然后使用1 end表示项目计数吗?SUMCASE WHEN DATE\u FORMATo.created\u DATE,“%Y-%m-%d”>='2012-06-25'和日期格式。创建日期,%Y-%m-%d'应该有效。如果在没有分组依据的情况下运行查询,s.price是否包含该范围内的非空值?当我运行不带group by子句的查询时,两列中只有一条记录的值均为空。感谢它对“of item salled Week”和“of item salled Month”工作正常,但对“Total_price Week”和“Total_price Month”显示为空,这很奇怪,您确定要使用s.price end作为价格,然后使用1 end作为项目计数吗?SUMCASE WHEN DATE_FORMATo.created_DATE,'%Y-%m-%d'>='2012-06-25'和DATE_FORMATo.created_DATE,'%Y-%m-%d',应该可以使用。如果在没有分组依据的情况下运行查询,s.price是否包含该范围的非空值?当我运行不带group by子句的查询时,它将仅显示两列中具有空值的一条记录。:-当我运行此查询时,它不会在结果中显示任何记录。此查询中的问题是,当其中一个子查询未返回任何记录时,则所有结果都不会返回任何记录。您可能要将其更改为“左连接”以解决无记录问题。@Brent Baisley是的,使用“左连接”可以很好地工作,但Andomar解决方案比此更快。非常感谢您的帮助:-当我运行此查询时,结果中不会显示任何记录。此查询中的问题是,当其中一个子查询返回无记录时,则所有结果都将返回无记录您可能想将其更改为“左连接”以解决无记录问题。@Brent Baisley是的,使用“左连接”可以很好地工作,但Andomar解决方案比此更快。非常感谢您的帮助