简体   繁体   English

我想在第一张表中获取不在第二张表中的行

[英]I want to fetch rows in a first table that are not in a second table

I have a table, let's call it fees , and two other tables, let's call them transactions and breakdowns . 我有一个表,我们称它为“ fees ,还有另外两个表,我们称它们为“ transactionsbreakdowns

transactions table saves the general summary of payments received while breakdowns table saves the breakdown of the payments. transactions表保存收到的付款的一般摘要, breakdowns保存付款的明细。

breakdowns table looks something like this 故障表看起来像这样

id | transaction_id | fee_id

and fees table looks something like this 和费用表看起来像这样

id | amount | description

I'd like to fetch the fees in fees table that do not have records in breakdowns table based on the transaction_id 我想根据Transactions_id在费用明细表中获取没有明细表中记录的费用

SELECT * FROM fees LEFT JOIN breakdowns ON (fees.id = breakdowns.fee_id) 
WHERE breakdowns.transaction_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

The code above returns empty rows 上面的代码返回空行

Use not exists : 使用not exists

SELECT f.*
FROM fees f
WHERE NOT EXISTS (SELECT 1
                  FROM breakdowns b
                  WHERE b.fee_id = f.id AND
                        b.transaction_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                 );

You can use a LEFT JOIN as well, moving the condition on breakdowns to the ON clause: 您也可以使用LEFT JOIN ,将breakdowns条件移至ON子句:

SELECT *
FROM fees f LEFT JOIN
     breakdowns b
     ON b.fee_id = f.id AND  
        b.transaction_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

However, I think NOT EXISTS more directly captures the logic you want. 但是,我认为NOT EXISTS更直接地捕获您想要的逻辑。 Also, I see no reason to return the NULL values for the columns in breakdowns . 另外,我认为没有理由为breakdowns的列返回NULL值。

try this 尝试这个

$data['showrecord'] = DB::table('fees')
           ->where('breakdown.transaction_id',"=",'xxxxx')
           ->leftjoin('breakdown', 'breakdown.fee_id', '=', 'fees.id')
           ->get();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果第一个表中不存在,我想从第二个表中获取数据 - I want fetch data from second table if not present in first table 我想基于json-php中输出的第一张表行从第二张表获得输出结果 - I want to get output result from second table based on first table rows output in json-php 我想在表中获取多维数组 - I want to fetch multidimensional array in my table 我想从客户表中获取名字 - I want to fetch firstname from customer table 我想将数据库元素数组提取到表中 - I want to fetch array of database elements into table 两个表一个包含记录,第二个包含位置我想显示第一个表记录而不连接 - Two tables one contain records and second conatain location i want to show first table record with out join 我想从一个表中获取所有数据并将它们插入另一个表中,但只有第一行插入多次而不是所有行 - I want to fetch all data from a table and insert them in another table but only first row is inserting multiple time instead of all row 当我将第二个表加入第一个表时的总和不正确 - Incorrect sum of when I join a second table to first table 检查第一个表中的行,将它们与第二个表中的行进行比较并输出匹配项 - Check rows in first table, compare them with rows from the second table and output a match 根据第一个表中的值从第二个表中获取和回显数据 - Fetch and echo data from second table based on value inside first table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM