MySQL:显示不包含特定值的行
MySQL: show a row that not contains a specific value
我无法在 MySQL 中显示不包含值的特定行。
首先这是我的两个 table:
这是一个数组,其中包含 table 的信息:
账单table:
[0] => Array
(
[id] => 1
[reference] => #001001
[seat_id] => 101
[client_id] => 10200
)
[1] => Array
(
[id] => 2
[reference] => #001002
[seat_id] => 102
[client_id] => 10400
)
[2] => Array
(
[id] => 3
[reference] => #001003
[seat_id] => 103
[client_id] => 10600
)
Accounting_seats table:
[0] => Array
(
[0] => Array
(
[id] => 1
[seat_id] => 101
[account_id] => taxes_qty
[value] => 0.99
[client_id] => 10200
)
[1] => Array
(
[id] => 2
[seat_id] => 101
[account_id] => tax_base
[value] => 4
[client_id] => 10200
)
[2] => Array
(
[id] => 3
[seat_id] => 101
[account_id] => total
[value] => 4.99
[client_id] => 10200
)
)
[1] => Array
(
[0] => Array
(
[id] => 4
[seat_id] => 102
[account_id] => taxes_qty
[value] => 2.00
[client_id] => 10400
)
[1] => Array
(
[id] => 5
[seat_id] => 102
[account_id] => tax_base
[value] => 8.00
[client_id] => 10400
)
[2] => Array
(
[id] => 6
[seat_id] => 102
[account_id] => shipping_cost
[value] => 2.00
[client_id] => 10400
)
[3] => Array
(
[id] => 7
[seat_id] => 102
[account_id] => total
[value] => 12.00
[client_id] => 10400
)
)
[2] => Array
(
[0] => Array
(
[id] => 8
[seat_id] => 103
[account_id] => taxes_qty
[value] => 3
[client_id] => 10600
)
[1] => Array
(
[id] => 9
[seat_id] => 103
[account_id] => tax_base
[value] => 7
[client_id] => 10600
)
[2] => Array
(
[id] => 10
[seat_id] => 103
[account_id] => shipping_cost
[value] => 3.99
[client_id] => 10600
)
[3] => Array
(
[id] => 11
[seat_id] => 103
[account_id] => total
[value] => 13.99
[client_id] => 10600
)
)
问题是我无法显示 accounting_seats 不包含值“shipping_cost”的帐单。只显示 shipping_cost 的 2 个账单或所有账单,但我需要没有 shiipping_cost 价值的账单。
SELECT reference, seat_id FROM bills WHERE seat_id IN(101,102,103) AND seat_id IN (SELECT seat_id FROM accounting_seats WHERE account != 'shipping_cost');
非常感谢您的帮助!
试试这个:
SELECT reference, seat_id
FROM bills
WHERE seat_id IN(101,102,103)
AND seat_id NOT IN (SELECT seat_id FROM accounting_seats WHERE account = 'shipping_cost');
我会推荐 not exists
:
select reference, seat_id
from bills b
where
b.seat_id in(101,102,103)
and not exists (
select 1
from accounting_seats acs
where acs.account = 'shipping_cost' and acs.seat_id = b.seat_id
)
与not in
相比,not exists
通常在基础table的大小增加时扩展得更好(这里是accounting_seats
):上面的查询会利用accounting_seats(account, seat_id)
上的索引。这种方法的另一个好处是 not exists
对于 seat_id
中的 null
值是安全的,而 not in
则不是。
我无法在 MySQL 中显示不包含值的特定行。
首先这是我的两个 table:
这是一个数组,其中包含 table 的信息:
账单table:
[0] => Array
(
[id] => 1
[reference] => #001001
[seat_id] => 101
[client_id] => 10200
)
[1] => Array
(
[id] => 2
[reference] => #001002
[seat_id] => 102
[client_id] => 10400
)
[2] => Array
(
[id] => 3
[reference] => #001003
[seat_id] => 103
[client_id] => 10600
)
Accounting_seats table:
[0] => Array
(
[0] => Array
(
[id] => 1
[seat_id] => 101
[account_id] => taxes_qty
[value] => 0.99
[client_id] => 10200
)
[1] => Array
(
[id] => 2
[seat_id] => 101
[account_id] => tax_base
[value] => 4
[client_id] => 10200
)
[2] => Array
(
[id] => 3
[seat_id] => 101
[account_id] => total
[value] => 4.99
[client_id] => 10200
)
)
[1] => Array
(
[0] => Array
(
[id] => 4
[seat_id] => 102
[account_id] => taxes_qty
[value] => 2.00
[client_id] => 10400
)
[1] => Array
(
[id] => 5
[seat_id] => 102
[account_id] => tax_base
[value] => 8.00
[client_id] => 10400
)
[2] => Array
(
[id] => 6
[seat_id] => 102
[account_id] => shipping_cost
[value] => 2.00
[client_id] => 10400
)
[3] => Array
(
[id] => 7
[seat_id] => 102
[account_id] => total
[value] => 12.00
[client_id] => 10400
)
)
[2] => Array
(
[0] => Array
(
[id] => 8
[seat_id] => 103
[account_id] => taxes_qty
[value] => 3
[client_id] => 10600
)
[1] => Array
(
[id] => 9
[seat_id] => 103
[account_id] => tax_base
[value] => 7
[client_id] => 10600
)
[2] => Array
(
[id] => 10
[seat_id] => 103
[account_id] => shipping_cost
[value] => 3.99
[client_id] => 10600
)
[3] => Array
(
[id] => 11
[seat_id] => 103
[account_id] => total
[value] => 13.99
[client_id] => 10600
)
)
问题是我无法显示 accounting_seats 不包含值“shipping_cost”的帐单。只显示 shipping_cost 的 2 个账单或所有账单,但我需要没有 shiipping_cost 价值的账单。
SELECT reference, seat_id FROM bills WHERE seat_id IN(101,102,103) AND seat_id IN (SELECT seat_id FROM accounting_seats WHERE account != 'shipping_cost');
非常感谢您的帮助!
试试这个:
SELECT reference, seat_id
FROM bills
WHERE seat_id IN(101,102,103)
AND seat_id NOT IN (SELECT seat_id FROM accounting_seats WHERE account = 'shipping_cost');
我会推荐 not exists
:
select reference, seat_id
from bills b
where
b.seat_id in(101,102,103)
and not exists (
select 1
from accounting_seats acs
where acs.account = 'shipping_cost' and acs.seat_id = b.seat_id
)
与not in
相比,not exists
通常在基础table的大小增加时扩展得更好(这里是accounting_seats
):上面的查询会利用accounting_seats(account, seat_id)
上的索引。这种方法的另一个好处是 not exists
对于 seat_id
中的 null
值是安全的,而 not in
则不是。