[英]Select query to fetch 5 5 rows based on different column values in mysql
I have an Invoice table which contains invoice data in each row where an invoice can be in different state such as due, overdue, disputed and partially paid.我有一个发票表,其中每一行都包含发票数据,其中发票可以处于不同状态,例如到期、逾期、有争议和部分支付。 Below is the table structure:
下面是表结构:
Invoice_no![]() |
due_state![]() |
disputed_state![]() |
paid_state![]() |
---|---|---|---|
inv1 ![]() |
due![]() |
dispute_none![]() |
paid_none![]() |
inv2 ![]() |
overdue![]() |
disputed![]() |
partially_paid![]() |
inv3 ![]() |
due![]() |
disputed![]() |
partially_paid![]() |
I need to fetch 5 invoices which are in due state, 5 invoices which are in overdue state, 5 invoices which are in disputed state and 5 invoices which are in partially paid state in single mysql query.我需要在单个 mysql 查询中获取 5 张处于到期状态的发票、5 张处于逾期状态的发票、5 张处于有争议状态的发票和 5 张处于部分支付状态的发票。
Currently I'm running 5 different sqls to fetch results for each states.目前我正在运行 5 个不同的 sqls 来获取每个州的结果。 Please help me to create one sql to fetch invoices for each states in a single sql query.
请帮助我创建一个 sql 以在单个 sql 查询中获取每个州的发票。
I'm using mysql database.我正在使用 mysql 数据库。 Currently I'm doing it as
目前我正在这样做
Select * from table where due_state = 'due' limit 5;
Select * from table where due_state = 'overdue' limit 5;
Select * from table where diputed_state = 'disputed' limit 5;
Select * from table where paid_state = 'partially_paid' limit 5
Thanks in advance.提前致谢。
You can apply:您可以申请:
UNION ALL
if you want 20 records with potential duplicates UNION ALL
如果你想要 20 条可能重复的记录UNION
if you are allowing less than 20 unique records, among which there can be records that satisfy more conditions at the same time. UNION
如果你允许少于 20 个唯一记录,其中可以有同时满足更多条件的记录。Select * from table where due_state = 'due' limit 5
UNION [ALL]
Select * from table where due_state = 'overdue' limit 5
UNION [ALL]
Select * from table where diputed_state = 'disputed' limit 5
UNION [ALL]
Select * from table where paid_state = 'partially_paid' limit 5
Note: [ALL]
is the optional keyword to be added without brackets or removed completely from the final query.注意:
[ALL]
是可选关键字,不带括号添加或从最终查询中完全删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.