简体   繁体   English

两个MySQL查询合二为一,第二个查询基于第一个查询

[英]Two MySQL queries in one, with the second query based on first query

I am struggling with this one. 我正在努力解决这个问题。

I have a table called customers, and a table called sales. 我有一个名为customers的表,还有一个名为sales的表。 I want to be able to list for example, all the customers with who have sales from 31 Dec 2010 and earlier - however at the same time exclude the customer if the was a sale made after 31 Dec 2010. 我希望能够列出例如2010年12月31日及之前销售的所有客户 - 但如果是2010年12月31日之后的销售,则同时将客户排除在外。

At the moment I have 目前我有

SELECT * FROM sales WHERE date <= '2010-12-31 23:59:59' ORDER BY sale_id DESC

Which selects the sales on or before my set date. 在我设定的日期或之前选择销售。

Then I have another query 然后我有另一个查询

SELECT * FROM sales WHERE customer_id='$result1[customer_id]' AND date >= '2010-12-31 23:59:59'

This now finds the sales using the customer_id value from the original query to find which customers have sales after the my search date. 现在,使用原始查询中的customer_id值查找销售额,以查找在我的搜索日期之后哪些客户具有销售额。 I then use a variable if this second query returns a result, for example.. 然后我使用变量,如果第二个查询返回结果,例如..

$query2 = mysql_db_query($dbname, "SELECT * FROM sales WHERE customer_id='$r1[customer_id]' AND date >= '2010-12-31 23:59:59'");
$counted = mysql_num_rows($query2); 
if($counted > 0){
    $hidesale="do";
}

which stops the original query from displaying a result. 这会阻止原始查询显示结果。

However this obviously can take some time as it needs to be select every sale before 31 Dec 2010 then run a second query under that sale to find it out if the same customer has any future sales determining if it should be shown or not. 然而,这显然可能需要一些时间,因为它需要在2010年12月31日之前选择每次销售,然后在该销售下运行第二个查询以找出它,如果同一客户有任何未来销售确定是否应该显示。

Is it possible to get the above into a single MySQL query, that way I can also limit results to create a page system so that all the results aren't on a single page? 是否有可能将上述内容纳入单个MySQL查询中,这样我还可以限制结果以创建页面系统,以便所有结果都不在单个页面上?

You can select customers who have sales before 2010-12-31 that doesn't have any sales after 2010-12-31 by using NOT EXISTS like below 您可以选择在2010-12-31之前销售但在2010-12-31之后没有任何销售的客户,使用下面的NOT EXISTS

SELECT * FROM sales s1 WHERE date <= '2010-12-31 23:59:59'
AND NOT EXISTS(SELECT customer_id FROM sales s2 
                 WHERE s2.customer_id = s1.customer_id
                 date > '2010-12-31 23:59:59')
ORDER BY sale_id DESC

or you can use LEFT JOIN solution 或者您可以使用LEFT JOIN解决方案

SELECT s1.*
FROM sales s1
LEFT JOIN sales s2
ON (s1.customer_id = s2.customer_id AND s2.date > '2010-12-31 23:59:59')
WHERE s1.date <= '2010-12-31 23:59:59'
AND s2.customer_id IS NULL
ORDER BY s1.sale_id DESC

to speed things up you can index customer_id column and date column and sale_id column by using 为了加快速度,您可以使用以下方法索引customer_id列和日期列以及sale_id列

CREATE INDEX sales_customer_id_index ON sales(customer_id);
CREATE INDEX sales_date_index ON sales(date);
CREATE INDEX sales_sale_id_index ON sales(sale_id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想使用 MySQL 中第二个查询中第一个查询的列将两个查询合二为一 - I want to use two queries into one using a column from first query in the second one in MySQL 加入两个mysql查询,以第二个查询的结果对第一个查询进行排序 - join two mysql queries to order the first query by the result of the second 将两个查询合并为一个需要第一个查询中的值的查询 - Combine the two queries into one that needs a value from second query in first 基于第一个的第二个 sql 查询 - second sql query based on the first one MySQL将两个查询合并为一个查询 - mysql combine two queries into one query MySQL在一个Query中组合了两个UPDATE查询 - MySQL combine two UPDATE Queries in one Query 加入 2 个 mysql 查询,其中第二个查询依赖于第一个的 output - Join 2 mysql queries, where the second query depends on the output of the first 是否可以连接到查询,但获得第二个查询之前的第一个查询结果? - Is it possible to connect to queries but get the results of first query before second one? Laravel中的MySQL子查询,如何联接查询并将第二查询的结果作为新列添加到第一查询? - MySQL sub query in Laravel, how to join queries and add second query's result as a new column to first query? 根据第一个查询答案联接两个SQL查询 - Join two SQL Queries based on first query answer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM