帮忙看下这个要求要怎么实现

hyz_cs 2014-03-15 12:46:29


mysql> select * , ( select owner from terminal where terminal.id = terminalinfo.terminal limit 1 ) as userid from terminalinfo where InfoType > 2;
+----+----------+----------+---------+--------+--------------------------------+----------+---------------------+--------+
| ID | InfoType | Terminal | PrgType | PrgPos | FileDir | FileSize | FinishTime | userid |
+----+----------+----------+---------+--------+--------------------------------+----------+---------------------+--------+
| 1 | 20 | 422 | 0 | 0 | 0=JH-A3,1=0,2=512,3=64,4=2,5=2 | 0 | 2014-03-15 12:32:26 | 81 |
+----+----------+----------+---------+--------+--------------------------------+----------+---------------------+--------+



这个没问题,问题是我想在后面的个条件中使用"userid" 这个字段,像下面这条语句



select * , ( select owner from terminal where terminal.id = terminalinfo.terminal limit 1 ) as userid from terminalinfo where find_in_set(userid,'81,82') and InfoType > 2;
1054 - Unknown column 'userid' in 'where clause'
mysql>



但是却是错误的.
...全文
91 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
hyz_cs 2014-03-15
  • 打赏
  • 举报
回复
也就是在后面的个条件中加上 find_in_set(userid,'81,82')
benluobo 2014-03-15
  • 打赏
  • 举报
回复
那你改成 find_in_set(userid,'81,82') > 0 刚才写错了 要不就是 find_in_set(userid,'81,82') >= 1
benluobo 2014-03-15
  • 打赏
  • 举报
回复
FIND_IN_SET(str,strlist) 假如字符串str 在由N 子链组成的字符串列表strlist 中, 则返回值的范围在 1 到 N 之间 。一个字符串列表就 是一个由一些被‘,’符号分开的自链组成的字符串。如果第一个参数是一个常数字符串,而第二个是type SET列,则 FIND_IN_SET() 函数被优化,使用比特计算。如果str不在strlist 或strlist 为空字符串,则返回值 为 0 。如任意一个参数为NULL,则返回值为 NULL。 这个函数在第一个参数包含一个逗号(‘,’)时将无法正常运 行。
hyz_cs 2014-03-15
  • 打赏
  • 举报
回复
引用 4 楼 benluobobo 的回复:
select * from (select * , ( select owner from terminal where terminal.id = terminalinfo.terminal limit 1 ) as userid from terminalinfo where InfoType > 2) A where find_in_set(userid,'81,82') > 1 这样就可以了 而且你的 find_in_set 好像没给条件吧
可以了,谢谢,有个条件啊,find_in_set 的条件就是 userid 在后面这几个值中间.
hyz_cs 2014-03-15
  • 打赏
  • 举报
回复
引用 2 楼 benluobobo 的回复:
userid 是你查出来的字段赋值的别名 SQL执行步骤中,where在select前面,所以不可能在where中使用不存在的别名 你可以在外面加一层查询,使用findinset就可以了


 select ( select * , ( select owner from terminal where terminal.id = terminalinfo.terminal limit 1 ) as userid from terminalinfo where InfoType > 2 ) where find_in_set(userid,'81,82') ; 
这样写不对.
benluobo 2014-03-15
  • 打赏
  • 举报
回复
select * from (select * , ( select owner from terminal where terminal.id = terminalinfo.terminal limit 1 ) as userid from terminalinfo where InfoType > 2) A where find_in_set(userid,'81,82') > 1 这样就可以了 而且你的 find_in_set 好像没给条件吧
hyz_cs 2014-03-15
  • 打赏
  • 举报
回复
引用 2 楼 benluobobo 的回复:
userid 是你查出来的字段赋值的别名 SQL执行步骤中,where在select前面,所以不可能在where中使用不存在的别名 你可以在外面加一层查询,使用findinset就可以了
要怎么写,能帮忙写个例子吗
benluobo 2014-03-15
  • 打赏
  • 举报
回复
userid 是你查出来的字段赋值的别名 SQL执行步骤中,where在select前面,所以不可能在where中使用不存在的别名 你可以在外面加一层查询,使用findinset就可以了

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧