简体   繁体   English

MySQL查询通过检查onother表中的其他两个属性从一个表中选择所有数据

[英]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.

相关问题 如何从一个表而不是两个表中将此MySQL查询更改为SELECT? - How to change this MySQL query to SELECT from one table instead of two? php \\ mysql在一个查询中从两个不相关的表中删除数据 - php\mysql delete data from two not related table in one query mysql join查询从两个表中选择行,一个表有多个与第一个表匹配的行 - mysql join query for select rows from two tables that one table is having multiple rows matching to the first table MySQL查询从多个表中选择,显示所有来自表1 +一些数据来自表2 - MySQL query select from multiple tables, display all from table1 + some data from table2 从一个条件为的表中检索数据,并在一个SELECT查询中检查另一个表中的记录 - Retrieve data from a table with a condition where and check the record in another table in one SELECT query 如何在mysql中通过一个查询从两个不同的表中选择两个记录 - How to select two record from two different table with one query in mysql MySQL的两个表,从一个表中选择所有值,比较是否存在,然后返回一个随机值 - Mysql two tables, select all values from one table, compare if exists and then return One random value mysql查询获取表1中的所有字段,并从表2中获取部分字段 - mysql query getting all fields in table 1 and partial in from table two MySQL查询以选择表2中与表1匹配的所有结果 - MySQL query to select all results from table 2 that match table 1 从一个MySQL请求中的另一个SELECT中选择一个表中的SELECT? - SELECT in a table from an other SELECT in one MySQL request?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM