Shizle Shizle - 5 months ago 10
MySQL Question

Using selected columns in a WHERE EXISTS query in MySQL

I have a table which is related to itself, with columns vin, type and sn. I want to find all rows with type of 2 that have only one row with type of 1; I tried this:

select count(*) as aggregate
from `data` as `t1`
where `type` = 2
and `last_service` < DATE_SUB(now(), INTERVAL 12 MONTH)
and exists (
select count(*) as data.count
from `data`
where count=1
and `type` = 1
)


but I keep getting the following error:


Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '
.count from
data
where count=1 and
type
= ?)' at line 1
.

Answer

There is syntax error because you use alias as complex identifier: as data.count. Try as data_count instead.

Comments