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 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决