[英]mysql query to select all data from one table by check the other two atributes in onother table
I have two (2) tables in one database, I want to display all members that are not paid for either payment: 我在一个数据库中有两(2)个表,我想显示所有未付款的会员:
I tried this but not working: 我尝试了这个,但是没有用:
SELECT members.*, payments.amount='', paid_for='fee';
Table 1 " members " with ID, ROLL NO and NAMES 表1带有ID,ROLL NO和NAMES的“ 成员 ”
Table 2 " payments " with ID ROLL NO, AMOUNT, PAID_FOR, DATE, STATUS, RECEIPT NO 表2的ID为ROLL NO,AMOUNT,PAID_FOR,DATE,STATUS,RECEIPT NO的“ 付款 ”
I want to select all where AMOUNT is empty , PAID FOR fee 我想选择AMOUNT为空,已付费的所有内容
That means if there no payment made, it display all members in database as not paid. 这意味着如果没有付款,它将数据库中的所有成员显示为未付款。
Something like this: 像这样:
SELECT m.ID,m.ROLL_NO,m.NAMES,p.AMOUNT,p.PAID_FOR,p.DATE,p.STATUS,p.RECEIPT_NO
FROM MEMBERS m INNER JOIN PAYMENTS p
ON m.ROLL_NO = p.ROLL_NO
WHERE (m.AMOUNT IS NULL OR m.AMOUNT='') AND p.PAID_FOR='fee'
GROUP BY m.NAMES;
If you want all members with no payment made , you may need use inner join
, and with your requirement, your condition should be and
, try following;) 如果您希望所有成员均不付款 ,则可能需要使用inner join
,并且根据您的要求,您的条件应为and
,请尝试以下;)
select distinct t1.*
from members t1
inner join payments t2
on t1.roll_no = t1.roll_no
and t2.paid_for = 'fee' and (t2.amount is null or t2.amount = '')
Or could use exists
like: 或可以使用exists
:
select t1.*
from members t1
where exists (
select 1
from payments t2
where t1.roll_no = t2.roll_no
and t2.paid_for = 'fee'
and (t2.amount is null or t2.amount = '')
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.