SQLite子查询语法/错误/与MySQL的区别

发布于 2024-08-30 23:56:47 字数 584 浏览 6 评论 0原文

我的印象是这是有效的 SQLite 语法:

SELECT
  *,
  (SELECT amount AS target 
     FROM target_money 
    WHERE start_year <= p.bill_year 
      AND start_month <= p.bill_month 
 ORDER BY start_year ASC, start_month ASC 
    LIMIT 1) AS target
FROM payments AS p;

但我想它不是,因为 SQLite 返回此错误:

没有这样的列:p.bill_year

我引用 p.bill_year 的方式有什么问题?
是的,我确信表 payment 包含一列 bill_year 。我疯了还是这只是有效的 SQL 语法?它可以在 MySQL 中工作不是吗?我没有任何其他 SQL 存在,所以我无法测试其他 SQLite,但我认为 SQLite 非常标准。

I was under the impression this is valid SQLite syntax:

SELECT
  *,
  (SELECT amount AS target 
     FROM target_money 
    WHERE start_year <= p.bill_year 
      AND start_month <= p.bill_month 
 ORDER BY start_year ASC, start_month ASC 
    LIMIT 1) AS target
FROM payments AS p;

But I guess it's not, because SQLite returns this error:

no such column: p.bill_year

What's wrong with how I refer to p.bill_year?
Yes, I am positive table payments hosts a column bill_year. Am I crazy or is this just valid SQL syntax? It would work in MySQL wouldn't it?? I don't have any other SQL present so I can't test others, but I thought SQLite was quite standardlike.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡莣 2024-09-06 23:56:47

谢谢,标记
您的查询在 SQLite 中运行良好:

>>> import sqlite3
>>> conn = sqlite3.connect(':memory:')
>>> c = conn.cursor()

>>> c.execute('CREATE TABLE payments (bill_year INT, bill_month INT);')
<sqlite3.Cursor object at 0x00C62CE0>
>>> conn.commit()

>>> c.execute("""CREATE TABLE target_money 
        (amount INT, start_year INT, start_month INT);""")
<sqlite3.Cursor object at 0x00C62CE0>
>>> conn.commit()

>>> c.execute("""
... SELECT
...   *,
...   (SELECT amount AS target
...    FROM target_money
...    WHERE start_year <= p.bill_year AND start_month <= p.bill_month
...    ORDER BY start_year ASC, start_month ASC
...    LIMIT 1) AS target
... FROM
...   payments AS p;
... """)
<sqlite3.Cursor object at 0x00C62CE0>
>>> c.fetchall()
[]

Thanks, Mark.
Your query works fine in SQLite:

>>> import sqlite3
>>> conn = sqlite3.connect(':memory:')
>>> c = conn.cursor()

>>> c.execute('CREATE TABLE payments (bill_year INT, bill_month INT);')
<sqlite3.Cursor object at 0x00C62CE0>
>>> conn.commit()

>>> c.execute("""CREATE TABLE target_money 
        (amount INT, start_year INT, start_month INT);""")
<sqlite3.Cursor object at 0x00C62CE0>
>>> conn.commit()

>>> c.execute("""
... SELECT
...   *,
...   (SELECT amount AS target
...    FROM target_money
...    WHERE start_year <= p.bill_year AND start_month <= p.bill_month
...    ORDER BY start_year ASC, start_month ASC
...    LIMIT 1) AS target
... FROM
...   payments AS p;
... """)
<sqlite3.Cursor object at 0x00C62CE0>
>>> c.fetchall()
[]
榕城若虚 2024-09-06 23:56:47

它可以在 MySQL 中工作:

CREATE TABLE payments (bill_year INT, bill_month INT);
CREATE TABLE target_money (amount INT, start_year INT, start_month INT);

SELECT
  *,
  (SELECT amount AS target
   FROM target_money
   WHERE start_year <= p.bill_year AND start_month <= p.bill_month
   ORDER BY start_year ASC, start_month ASC
   LIMIT 1) AS target
FROM
  payments AS p;

我想它也可以在 SQLite 中工作。我确信这里有人可以复制并复制粘贴上面的内容来验证它......

It works in MySQL:

CREATE TABLE payments (bill_year INT, bill_month INT);
CREATE TABLE target_money (amount INT, start_year INT, start_month INT);

SELECT
  *,
  (SELECT amount AS target
   FROM target_money
   WHERE start_year <= p.bill_year AND start_month <= p.bill_month
   ORDER BY start_year ASC, start_month ASC
   LIMIT 1) AS target
FROM
  payments AS p;

I'd imagine that it would work in SQLite too. I'm sure someone here can copy & paste the above to verify it...

吲‖鸣 2024-09-06 23:56:47

我做了一些测试来确认相关子查询不适用于 sqlite2,但适用于 sqlite3,这似乎是一种情况。问题是官方文档对此只字未提。 sqlite2 在某些奇怪的语法下支持相关子查询的可能性仍然很小。这就是我在不深入 sqlite2 源代码的情况下所能知道的。

I make some tests to confirm that correlated subqueries dosn't work on sqlite2, but works on sqlite3, and that seems to be a case. The problem is that official documentation says nothing about it. There is still a small possibility that correlated subqueries are supported in sqlite2 under some odd syntax. Thats all I can tell without getting into sqlite2 source code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文