douqiangchuai7674 2016-06-28 21:03
浏览 48
已采纳

在MySQL中的WHERE EXISTS查询中使用选定的列

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 fromdatawhere count=1 andtype= ?)' at line 1.

  • 写回答

2条回答 默认 最新

  • donpvtzuux37724 2016-06-28 21:09
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)