简体   繁体   中英

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:

I tried this but not working:

SELECT members.*, payments.amount='', paid_for='fee';

Table 1 " members " with ID, ROLL NO and NAMES

Table 2 " payments " with ID ROLL NO, AMOUNT, PAID_FOR, DATE, STATUS, RECEIPT NO

I want to select all where AMOUNT is empty , PAID FOR fee

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;)

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:

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 = '')
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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