为啥我不能从我的查询中选择?
Posted
技术标签:
【中文标题】为啥我不能从我的查询中选择?【英文标题】:Why can't I select from my query?为什么我不能从我的查询中选择? 【发布时间】:2011-05-12 01:56:41 【问题描述】:我有一个类似这样的查询(不,我的表和字段不是由这些名称调用的,但结构是相同的)-
SELECT table1.id, table2.id, table1.phone1
FROM table1 LEFT OUTER JOIN
table3 ON table3.id = table1.id LEFT OUTER JOIN
table2 ON table3.id2 = table2.id
WHERE table1.phone1 IS NOT NULL AND LTRIM(RTRIM(table1.phone1)) <> ''
UNION
SELECT table1.id, table2.id, table1.phone2
FROM table1 LEFT OUTER JOIN
table3 ON table3.id = table1.id LEFT OUTER JOIN
table2 ON table3.id2 = table2.id
WHERE table1.phone2 IS NOT NULL AND LTRIM(RTRIM(table1.phone2)) <> ''
UNION
SELECT table1.id, table2.id, table2.phone
FROM table1 LEFT OUTER JOIN
table3 ON table3.id = table1.id LEFT OUTER JOIN
table2 ON table3.id2 = table2.id
WHERE table2.phone IS NOT NULL AND LTRIM(RTRIM(table2.phone)) <> ''
运行良好。但是,当我尝试从中选择时(我最终会尝试使用 group bys 等进行操作,但现在只是尝试了一个简单的选择),如下所示:
SELECT * FROM
( SELECT table1.id, table2.id, table1.phone1
FROM table1 LEFT OUTER JOIN
table3 ON table3.id = table1.id LEFT OUTER JOIN
table2 ON table3.id2 = table2.id
WHERE table1.phone1 IS NOT NULL AND LTRIM(RTRIM(table1.phone1)) <> ''
UNION
SELECT table1.id, table2.id, table1.phone2
FROM table1 LEFT OUTER JOIN
table3 ON table3.id = table1.id LEFT OUTER JOIN
table2 ON table3.id2 = table2.id
WHERE table1.phone2 IS NOT NULL AND LTRIM(RTRIM(table1.phone2)) <> ''
UNION
SELECT table1.id, table2.id, table2.phone
FROM table1 LEFT OUTER JOIN
table3 ON table3.id = table1.id LEFT OUTER JOIN
table2 ON table3.id2 = table2.id
WHERE table2.phone IS NOT NULL AND LTRIM(RTRIM(table2.phone)) <> '' )
我得到错误:
Incorrect syntax near ')'.
我做错了什么?
【问题讨论】:
【参考方案1】:您错过了子选择的命名。以...) sub_query_name
之类的方式结束。
【讨论】:
+1:我知道现在是早上,我看的时候没有想到。 非常感谢!我知道这是我忘记的东西……现在可以了:)以上是关于为啥我不能从我的查询中选择?的主要内容,如果未能解决你的问题,请参考以下文章
为啥我的 SQLite 选择查询不能在应用程序中运行,而是在 Android Studio 上的 Database Inspector 上运行?