Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

Checking the item status in the main detail in MSSQL

$
0
0

I have three tables, namely:

Product ProductVariant SizeVariant

Product is the master table , productvariant is its child table of it and sizevariant is productvariant's child table.

All these tables have column of status which indicates whether they are alive or not. I want to check that whether there are any products whose productvariants and sizevariants status is dead but the product itself is alive.

select * from product p where p.status = 'alive' and not exists (select 1 from productvariant where productid = p.id and status='alive')

should get you all products that don't have an 'alive' variant. following the same formula, you could also check for product variants without any 'alive' size variants:

select * from productvariant pv where p.status = 'alive' not exists (select 1 from sizevariant where productvariantid = pv.id and status='alive')


Viewing all articles
Browse latest Browse all 3160

Trending Articles