简体   繁体   English

如何从两个 sql 查询结果中获取结果

[英]How to get results from two sql query results

I"m using SET @runtot:=0 at the top of my SQL code and it gets 2 query results:我在 SQL 代码的顶部使用SET @runtot:=0并得到 2 个查询结果:

The first one is:第一个是:

MySQL returned an empty result set (ie zero rows). MySQL 返回一个空结果集(即零行)。 (Query took 0.0002 seconds.) (查询耗时 0.0002 秒。)

The second is:第二个是:

Showing rows 0 - 7 (8 total, Query took 0.0058 seconds.)显示第 0 - 7 行(共 8 行,查询耗时 0.0058 秒。)

how can i get the results of the second query only?我怎样才能只得到第二个查询的结果?

here is my SQL code:这是我的 SQL 代码:

SET @runtot:=0;

SELECT 
fname,
lname,
guests,
phone

FROM (

(SELECT *, 0 AS rt FROM guests_table WHERE status = 2)
UNION
 (SELECT *, (@runtot := @runtot + rsvp.guests) AS rt
 FROM 
    (SELECT *
     FROM  guests_table
     WHERE status != 2
     ) AS rsvp
WHERE @runtot + rsvp.guests <= 4)
    )rsvp

ORDER BY rsvp.id ASC

You can set the parameters in the query:您可以在查询中设置参数:

SELECT fname, lname, guests, phone
FROM ((SELECT *, 0 AS rt FROM guests_table WHERE status = 2)
      UNION
      (SELECT *, (@runtot := @runtot + rsvp.guests) AS rt
       FROM (SELECT *
             FROM  guests_table
             WHERE status != 2
            ) AS rsvp
       WHERE @runtot + rsvp.guests <= 4)
      ) rsvp CROSS JOIN
      (SELECT @runtot := 0) params
ORDER BY rsvp.id ASC;

Note that the use of variables in SELECT queries like this has been deprecated.请注意,在SELECT查询中使用变量已被弃用。 In MySQL 8+, you should be using window functions.在 MySQL 8+ 中,您应该使用 window 函数。

In addition, the "running sum" is reading the rows in an arbitrary order.此外,“运行总和”是以任意顺序读取行。 There is no guarantee that the result will be in the same order as the results.无法保证结果与结果的顺序相同。

If you want further help with the query, ask a new question.如果您需要有关查询的进一步帮助,请提出一个问题。 Provide sample data, desired results, and a clear explanation of the logic.提供样本数据、期望的结果和清晰的逻辑解释。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM