我正在尝试这样的查询:
DELETE FROM term_hierarchy AS th
WHERE th.parent = 1015 AND th.tid IN (
SELECT DISTINCT(th1.tid)
FROM term_hierarchy AS th1
INNER JOIN term_hierarchy AS th2 ON (th1.tid = th2.tid AND th2.parent != 1015)
WHERE th1.parent = 1015
);
Run Code Online (Sandbox Code Playgroud)
你可以告诉我,如果同一个tid有其他父母,我想删除1015的父关系.但是,这会产生语法错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS th
WHERE th.parent = 1015 AND th.tid IN (
SELECT DISTINCT(th1.tid)
FROM ter' at line …Run Code Online (Sandbox Code Playgroud) 我曾尝试多次使用子查询和IN表达式编写查询语句.但我从来没有成功过.
我总是得到异常,"关键字'IN'附近的语法错误",查询语句是这样构建的,
SELECT t0.ID, t0.NAME
FROM EMPLOYEE t0
WHERE IN (SELECT ?
FROM PROJECT t2, EMPLOYEE t1
WHERE ((t2.NAME = ?) AND (t1.ID = t2.project)))
Run Code Online (Sandbox Code Playgroud)
我知道'IN'失败前的这个词.
你有没有写过这样的问题?有什么建议吗?
有没有办法(没有JOIN)WHERE在2列(OR)IN子查询上使用子句?目前,我正在做
WHERE 'col1' IN
(
SELECT id FROM table
) OR 'col2' IN
(
SELECT id FROM table
)
Run Code Online (Sandbox Code Playgroud)
而且我相信我能做得更好:).我也试过,WHERE ('col1', 'col2') IN <subquery>但MySQL说:Operand should contain 2 column(s)
谢谢你的帮助.
编辑:通过"不加入",我的意思是我正在进行许多加入:http://pastebin.com/bRfD21W9 ,正如您所看到的,子查询位于另一个表上.
假设有两个表:
Table A: A1, A2, A_Other
Table B: B1, B2, B_Other
Run Code Online (Sandbox Code Playgroud)
在以下示例中, is something 是针对固定值检查的条件,例如 = 'ABC' 或 < 45.
我写了一个如下(1)的查询:
Select * from A
Where A1 IN (
Select Distinct B1 from B
Where B2 is something
And A2 is something
);
Run Code Online (Sandbox Code Playgroud)
我真正想写的是(2):
Select * from A
Where A1 IN (
Select Distinct B1 from B
Where B2 is something
)
And A2 is something;
Run Code Online (Sandbox Code Playgroud)
奇怪的是,两个查询都返回了相同的结果.在查看查询1的解释计划时,看起来就像执行子查询时一样,因为条件不适用于子查询,所以它被推迟用作主查询结果的过滤器.A2 …
我是IQueryable,lambda表达式和LINQ的新手.我想在子句中放一个子查询,如下所示:
示例代码:
SELECT * FROM CLIENT c WHERE c.ETAT IN (
SELECT DDV_COLUMN_VAL FROM DATA_DICT_VAL
WHERE TBI_TABLE_NAME = 'CLIENT' AND DD_COLUMN_NAME = 'STATUS'
AND DDV_COLUMN_VAL_LANG_DSC_1 LIKE ('ac%'))
Run Code Online (Sandbox Code Playgroud)
我如何在LINQ中翻译它?
以下查询返回2036行:
SELECT "FooUID" from "Foo" f
LEFT JOIN "Bar" b ON f."BarUID" = b."BarUID"
WHERE f."BarUID" IS NOT NULL AND b."BarUID" IS NULL
Run Code Online (Sandbox Code Playgroud)
但以下语句仅更新了1870行:
UPDATE "Foo" f1 set "BarUID" = 'aNewUID'
WHERE f1."FooUID" IN (
SELECT f2."FooUID" from "Foo" f2
LEFT JOIN "Bar" b ON f2."BarUID" = b."BarUID"
WHERE f2."BarUID" IS NOT NULL AND b."BarUID" IS NULL
)
Run Code Online (Sandbox Code Playgroud)
这怎么可能?
编辑1:第一个查询继续返回166行,第二个查询继续更新0行.
编辑2:
在下文中,嵌套查询返回包含UID的行,但外部查询返回0行.
SELECT * from "Foo" f1
WHERE f1."FooUID" = (
SELECT f2."FooUID" FROM "Foo" f2
LEFT JOIN "Bar" …Run Code Online (Sandbox Code Playgroud) 以下MySQL查询:
select `userID` as uID,
(select `siteID` from `users` where `userID` = uID) as `sID`,
from `actions`
where `sID` in (select `siteID` from `sites` where `foo` = "bar")
order by `timestamp` desc limit 100
Run Code Online (Sandbox Code Playgroud)
...返回错误:
Unknown column 'sID' in 'IN/ALL/ANY subquery'
Run Code Online (Sandbox Code Playgroud)
我不明白我在这里做错了什么.这个sID东西不应该是一个列,而是我通过执行创建的'别名'(这叫做什么?)(select siteID from users where userID = uID) as sID.它甚至不在IN子查询中.
有任何想法吗?
编辑: @Roland:感谢您的评论.我有三张桌子actions,users和sites.该表actions包含一个userID字段,该字段对应于表中的条目users.此表(users)中的每个用户都有一个siteID.我正在尝试从actions表中选择最新的操作,并将它们链接到users和 …
我遇到了这个有趣的行为.我看到左连接是要走的路,但仍然希望将其清除.是设计中的错误还是行为?有什么解释吗?
当我从左表中选择记录时,右表上子查询的结果中不存在值,如果子查询结果为空,则不返回预期的"缺失"记录.我期望将此查询编写为等效的两种方法.
谢谢!
declare @left table (id int not null primary key identity(1,1), ref int null)
declare @right table (id int not null primary key identity(1,1), ref int null)
insert @left (ref) values (1)
insert @left (ref) values (2)
insert @right (ref) values (1)
insert @right (ref) values (null)
print 'unexpected empty resultset:'
select * from @left
where ref not in (select ref from @right)
print 'expected result - ref 2:'
select * from @left
where ref not in (select …Run Code Online (Sandbox Code Playgroud) 我有两个表,位置和位置组
CREATE TABLE locations (
location_id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(63) UNIQUE NOT NULL
);
INSERT INTO locations (name)
VALUES
('london'),
('bristol'),
('exeter');
CREATE TABLE location_groups (
location_group_id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
location_ids VARCHAR(255) NOT NULL,
user_ids VARCHAR(255) NOT NULL,
name VARCHAR(63) NOT NULL,
);
INSERT INTO location_groups (location_ids, user_ids, name)
VALUES
('1', '1,2,4', 'south east'),
('2,3', '2', 'south west');
Run Code Online (Sandbox Code Playgroud)
我想要做的是返回给定 user_id 存在的所有 location_groups 的所有 location_ids。我正在使用 CSV 将 location_ids 和 user_ids 存储在 location_groups 表中。我知道这不是规范化的,但这就是数据库的方式,它超出了我的控制。
我目前的查询是:
SELECT location_id …Run Code Online (Sandbox Code Playgroud) 我给出了3个SQL查询:
前2个查询占用不到0.5秒,但第三个查询类似于包含1st作为子查询的2nd,大约需要8秒.
我根据自己的需要为表编制索引,但时间并没有减少.
有人可以给我一个解决方案或提供一些解释这种行为.
谢谢!
想象一下查询
SELECT Col_A FROM TABLE_A WHERE Col_A IN (SELECT Col_A FROM TABLE_B)
Run Code Online (Sandbox Code Playgroud)
除此之外,TABLE_B没有Col_A列; 只有TABLE_A拥有它.我在Oracle 12中尝试了这个,我不确定它的版本有多远,但它看起来像是返回显示TABLE_A中所有Col_A数据的有效结果.作为开发人员,我期待这样的事情抛出一个错误,因为内部查询甚至不能再次访问TABLE_A.有人可以解释为什么或在哪里我们会使用像上面那样的查询情况,因为我几乎觉得它应该是系统中的错误.
如何重构此查询:
SELECT * FROM tbl t
WHERE (
t.id IN <subquery1>
OR t.id IN <subquery2>
OR t.id IN <subquery3>
)
Run Code Online (Sandbox Code Playgroud)
...变成看起来更像下面的东西:
SELECT * FROM tbl t
WHERE t.id IN (<subquery1> OR <subquery2> OR <subquery3>)
Run Code Online (Sandbox Code Playgroud)
注意:所有 3 个子查询都从相同的 中tbl t选择,但它们各自选择不同的列。
用一些具体的例子进一步阐明子查询:
SELECT col1 FROM tbl WHERE value=100SELECT col2 FROM tbl WHERE value=200SELECT col3 FROM tbl WHERE value=300表结构:
CREATE TABLE tbl (
id INTEGER PRIMARY KEY,
col1 INTEGER not null,
col2 INTEGER …Run Code Online (Sandbox Code Playgroud) 我做了这样的查询
SELECT *
FROM TABLE_A
WHERE 1=1
AND ID_NO IN (
SELECT ID_NO
FROM TABLE_B
WHERE SEQ = '1'
)
Run Code Online (Sandbox Code Playgroud)
问题是TABLE_B中没有列'ID_NO'.所以我期待查询不起作用.但是这个查询有效.我不明白为什么.
为什么不引起错误?
in-subquery ×13
subquery ×8
sql ×7
mysql ×6
oracle ×2
postgresql ×2
criteria-api ×1
jpa ×1
jpa-2.0 ×1
left-join ×1
linq-to-sql ×1
null ×1
select ×1
sql-delete ×1
where-clause ×1