在sql字段中搜索

时间:2016-10-06 19:35:49

标签: sql sql-server sql-server-2008

我想在sql字段中搜索,该字段不是加入的一部分,而是一个外部表,它与其中一个连接表的ID有关。

这是我的查询,我正在尝试一些搜索

SELECT sdate,stime (select catid from 
users where id = OM.id) as catid
FROM 
table2 AS OM 
INNER JOIN table2 a ON a.pid = OM.pid 
where 1=1 
AND (select catid from 
users where id = OM.id) = '120'

这是我收到错误的地方

AND(从中选择catid     id = OM.id)=' 120'

的用户
[Err] 42000 - [SQL Server]Incorrect syntax near the keyword 'AND'.
42000 - [SQL Server]Incorrect syntax near '='.

2 个答案:

答案 0 :(得分:0)

如何在stime之后添加逗号?

更改

SELECT sdate, stime (select catid from 
users where id = OM.id) as catid
FROM ...

SELECT sdate, stime, (select catid from 
users where id = OM.id) as catid
FROM ...

答案 1 :(得分:0)

这里有一个逻辑缺陷。您的子查询中可能会收到多行。你为什么不加入用户而不是这个奇怪的子查询?

SELECT sdate
    , stime
    , u.catid
FROM 
table2 AS OM 
INNER JOIN table2 a ON a.pid = OM.pid 
inner join user u on u.id = OM.id
where u.catid = '120'