mysql - 将列别名分配给条件在 sqlite 中不起作用的地方

标签 mysql sql sqlite

今天我遇到一个问题,下面的sql查询在sqlite中不起作用,但在mysql中起作用。

mysql和sqlite具有相同的表结构

Table structure
========================

tablet_order |  framework_item_id | framework_parent_id

1            |           1        |            0

2            |           2        |            1

3            |           3        |            2

SQL:

SELECT tablet_order AS tablet, (

SELECT framework_item_id
FROM framework
WHERE tablet_order = tablet
) AS tablet1
FROM framework
WHERE framework_item_id =1

别名“tablet”被分配给tablet_order字段。这在 sqlite 中产生了错误,但在 mysql 中却没有。

谁能解释一下这个......

最佳答案

SQL 标准规定,您不能在同一 SELECT 子句中的其他表达式中使用列别名,并且 SQLite 正确实现了这一点。

为了能够区分两个不同的 tablet_order 值,您必须为至少一个表指定一个表别名:

SELECT tablet_order AS tablet,
       (SELECT framework_item_id
        FROM framework AS f2
        WHERE f2.tablet_order = framework.tablet_order
       ) AS tablet1
FROM framework
WHERE framework_item_id = 1

(子查询没有意义,因为您可以直接使用framework_item_id。您可能想通过其他列查找?)

关于mysql - 将列别名分配给条件在 sqlite 中不起作用的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18657748/

相关文章:

MYSQL,如何获取ID大于4的数据

mysql - 在我的 sql 查询中收到 'users_personals.user_id' 中的未知列 'where clause'

c# - 当前上下文中不存在 FileUpload1

.net - 在这种情况下如何使用 SQL Parameter

sql - SQL Server 2000 中最大 5 个数字的总和

PHP MySQL 选择最大行数返回错误的数字

sql - 从 00 :00:00 to 23:59:59 转换 DateTime 时间

c - SQLite:当主数据库有一个打开的准备语句时,为什么我不能分离数据库?

SQLite 循环语句?

python - sqlite3 set_authorizer 方法出现问题