简体   繁体   中英

Python & Sqlite3 - Subset one table then join on two other tables

I'm using python and sqlite3. I have 3 tables:

Table 1: Col A

Table 2: Col A | Col B

Table 3: Col B

I want the first 500k rows from Table 1 and any matching rows from Table 2 that have matching rows from Table 3. How do I do this? I was thinking something like this

conn = sqlite3.connect('database.sqlite')

      conn.execute('SELECT * FROM Table1 LIMIT 500000 AS sample
      LEFT JOIN Table2
      ON sample.A = Table2.A
      LEFT JOIN Table3 ON table2.B = Table3.B')

But I get this error: OperationalError: near "AS": syntax error

The result should be 500k rows with all columns found in all 3 Tables. Apologies if any of my wording is difficult to understand.

As @furas said, LIMIT has to be at the end of the complete statement.

What you actually want to do is most likely a subquery, like:

SELECT * FROM (SELECT * FROM Table1 LIMIT 500000) AS sample
  LEFT JOIN Table2
  ON sample.A = Table2.A
  LEFT JOIN Table3 ON table2.B = Table3.B

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM