Bạn mã cố gắng đếm số hàng trong đó tất cả các trường không phải là giá trị rỗng. Bạn nên sử dụng is not null
thay vì chỉ not null
.
Để đếm số lượng trường, hãy sử dụng cái này:
SELECT sum((listing_photo_1 IS NOT NULL) +
(listing_photo_2 IS NOT NULL) +
(listing_photo_3 IS NOT NULL) +
(listing_photo_4 IS NOT NULL) +
(listing_photo_5 IS NOT NULL) +
(listing_photo_6 IS NOT NULL) +
(listing_photo_7 IS NOT NULL) +
(listing_photo_8 IS NOT NULL)
) as total
from listings
WHERE pmpid = '$pmpid';
Để đếm số hàng:
SELECT count(*) as total
from listings
WHERE listing_photo_1 IS NOT NULL AND
listing_photo_2 IS NOT NULL AND
listing_photo_3 IS NOT NULL AND
listing_photo_4 IS NOT NULL AND
listing_photo_5 IS NOT NULL AND
listing_photo_6 IS NOT NULL AND
listing_photo_7 IS NOT NULL AND
listing_photo_8 IS NOT NULL AND
pmpid = '$pmpid'";
CHỈNH SỬA:
Nếu chúng trống, hãy sử dụng logic như sau:
SELECT sum((listing_photo_1 IS NOT NULL and listing_photo_1 <> '') +
(listing_photo_2 IS NOT NULL and listing_photo_2 <> '') +
(listing_photo_3 IS NOT NULL and listing_photo_3 <> '') +
(listing_photo_4 IS NOT NULL and listing_photo_4 <> '') +
(listing_photo_5 IS NOT NULL and listing_photo_5 <> '') +
(listing_photo_6 IS NOT NULL and listing_photo_6 <> '') +
(listing_photo_7 IS NOT NULL and listing_photo_7 <> '') +
(listing_photo_8 IS NOT NULL and listing_photo_8 <> '')
) as total
from listings
WHERE pmpid = '$pmpid';