Easy question, still I can’t figure it out. I have two tables.
Table items: id (INT), text (VARCHAR)
Table shared: type (VARCHAR), account (VARCHAR), id (INT)
Explanation:
-
Table items contains a list of tweets.
-
Table shared contains a list of all tweets that have been shared (sent to Twitter).
Now I need to have a query that gives me the oldest text (smallest “id”) from the items table that has not been used for a specific “type” and “account” in the shared table.
Example:
The database looks like this. There are two items in the items table and no items have been shared yet.
Table items: [[1, "First Tweet"], [2, "Second Tweet"]]
Table shared: []
Running the query will give back [1, “First Tweet”]. After that, I will insert a value in the shared table so it looks like this:
Table items: [[1, "First Tweet"], [2, "Second Tweet"]]
Table shared: [["twitter", "my-twitter-account", 1]]
Running the query again will give back [2, “Second Tweet”]. After that, I will insert a value in the shared table so it looks like this:
Table items: [[1, "First Tweet"], [2, "Second Tweet"]]
Table shared: [["twitter", "my-twitter-account", 1], ["twitter", "my-twitter-account", 2]]
I know I could do it by first pulling all “id”s from the items table, put them in an array and then check whether an “id” is in the shared table, but I think that is a horrible solution… the table will grow up to about 10,000 items max.
SOLUTION
Edit: I modified JW.’s code a bit and it works perfectly. Thanks!
My query:
SELECT a.id, a.text, MIN(a.id) ID
FROM items a
LEFT JOIN shared b
ON a.id = b.id
WHERE b.id IS NULL
try using
INSERT..INTO SELECT