HI, can I ask if I am doing right , I am counting licensekey or licenseregisterno that exists many times in
personal_key table.
SELECT (SELECT COUNT(*) FROM personal_key WHERE licensekey = "$key"
OR licenseregisterno = "$key") AS countkeys_registerno
FROM personal as p
inner join personal_key as pkeyreg
ON pkeyreg.id = p.personal_key_reg_id
where p.status IN ('valid','completed')
the best way to learn is not by asking for help, but by testing, fixing, testing again, checking the manual for the correct syntax, fixing, and testing again
SELECT COUNT(*)
FROM personal_key
WHERE licensekey = "$key"
OR licenseregisterno = "$key"
you will get back exactly one row, containing a count
now run this query by itself –
SELECT 'oh hai' AS countkeys_registerno
FROM personal as p
INNER
JOIN personal_key as pkeyreg
ON pkeyreg.id = p.personal_key_reg_id
WHERE p.status IN ('valid','completed')
and now please tell us how many rows of ‘oh hai’ did you get?
But is it possible that I will search lic-123 and count how many times it exist in that table, and personal_key.id is exist in the personal.personal_key_reg_id ?
in my example the personal.personal_key_reg_id = 1 then I can count lic-123 ?
SELECT (SELECT COUNT(*) FROM personal_key WHERE licensekey = "lic-123"
OR licenseregisterno = "lic-123") AS countkeys_registerno
FROM personal as p
inner join personal_key as pkeyreg
ON pkeyreg.id = p.personal_key_reg_id
where p.status IN ('completed')
GROUP BY countkeys_registerno;
result count = 2
Query 2
SELECT COUNT(*)
FROM personal as p
INNER
JOIN personal_key as pkeyreg
ON pkeyreg.id = p.personal_key_reg_id
WHERE p.status IN ('completed')
AND ( pkeyreg.licensekey = 'lic-123' OR pkeyreg.licenseregisterno = 'lic-123' )
result count = 1
I prefer in Query 1 because that is what I need, but still having question mark on my head if that is correct or logic error?
your Query 1 is wrong – the personal_key table in the SELECT clause is a different version of the personal_key table in the main query, even thought they’re the same table… also, the grouping is wrong
i might have been able to demonstrate this a little more clearly if your test data had been more robust
your Query 2 counts only 1 row now, so my ‘oh hai’ demonstration wouldn’t have worked
here is the bottom line – it seems you want to return a count for a specific license key, but does it matter what the status is? or do you want to count all statuses for that specific license key?