[英]How can I use the result of a SQL query in another SQL query
I'm writing a ticket program in vb.net using MySQL. 我正在使用MySQL在vb.net中编写票务程序。
What I want is to use the Statename that a ticket can have (open/closed/on hold/etc) that I get from query 1, and use it in query 2 to get a list of all statenames , with the result of the first query as first item. 我想是使用Statename的票证可以有(开启/关闭/暂停/等),我从查询中1得到的,并用它在查询2中得到的所有statenames的列表,与第一的结果作为第一项查询。
I now use 2 queries after each other, but i'm sure it can be done in 1 query. 我现在互相使用2个查询,但是我敢肯定它可以在1个查询中完成。
Query 1: 查询1:
SELECT StateName
FROM Tickets t
JOIN States s
ON s.stateID = t.StateID
WHERE TicketNumber = & *intTicketNumber*
--> Result = "Open" ->结果=“打开”
Query 2: 查询2:
SELECT StateName
from States
ORDER
BY(case when StateName = '" & *StrStateName* & "' then 0 else 1 end)
, StateName desc
The Desired result of the combined query should be: 组合查询的所需结果应为:
How Can I combine there 2 queries? 如何将两个查询结合在一起? Any help would really be appreciated!
任何帮助将不胜感激!
States
as your Leftmost table and do a LEFT JOIN
to the Tickets table, on stateID
and TicketNumber
. States
作为最左边的表,并在stateID
和TicketNumber
上对Tickets表进行LEFT JOIN
。 Match the TicketNumber
in the Join condition, instead of Where
. TicketNumber
,而不是Where
。 States
as leftmost table will ensure that all the StateName values will be considered. States
作为最左边的表格将确保所有StateName值都将被考虑。 t.StateId
will be NULL
(post LEFT JOINing), except for one StateName (assigned to the Ticket currently). t.StateId
将为NULL
(在LEFT JOINing之后),一个StateName(当前分配给Ticket)除外。 t.StateId
in Descending order. t.StateId
进行排序。 Ticket's current StateName will appear first in the list and rest of them sorted afterwards. Try with Left join instead: 尝试使用左连接:
SELECT s.StateName
FROM States AS s
LEFT JOIN Tickets AS t
ON s.stateID = t.StateID AND
t.TicketNumber = $intTicketNumber -- $intTicketNumber is the input variable
ORDER BY t.StateID DESC,
s.StateName DESC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.