Consider the following three MySQL tables:
tweets urls tweets_urls
--------------------------- --------------------- ----------------
tweet_id text spam url_id host spam tweet_id url_id
--------------------------- --------------------- ----------------
1 I love cnn.com 0 16 cnn.com 0 1 16
2 fox.com is fuk 0 17 fox.com 1 2 17
3 love me! 0 4 16
4 blah cnn.com 0
5 nice fox.com 0
I want to update tweets.spam by according to tweets_urls, meaning the output of the query should be
tweets
---------------------------
tweet_id text spam
---------------------------
1 I love cnn.com 0 <-- tweets_urls tells me tweet_id 1 has url_id 16
2 fox.com is fuk 1 in it, and the urls-table tells me that url 16
3 love me! 0 is not spam (spam = 0)
4 blah cnn.com 0
5 nice fox.com 1
I hope I’m making myself clear. I have been fiddling with it and now have something like this. I know it cannot be correct, but have no idea on how to start over. Do you?
UPDATE tweets SET spam = (
SELECT spam FROM urls
LEFT JOIN tweets_urls
WHERE urls.url_id = tweets_urls.url_id
)
Any help would be appreciated 🙂
For your given data, this query returns the result set…
But we’ve made some assumptions about what should be done when is more than one row in tweets_url for a given tweet_id, or when there is no matching url, etc.
If what you want is for a tweet to to marked as
“spam=1” whenever that tweet is found to be related to ANY url that is marked as “spam=1”, and otherwise, the tweet should be marked as “spam=0″…
This will set the spam column for every row in tweets, based on that rule…
If you want to leave the spam column alone (leave it set to whatever it is set to) and ONLY want to update a row where the value is currently set to 0 and should be set to 1, according to the “matching url has spam=1”, you could do this:
Note that predicate on the tweets table, we will ONLY be updating rows that have spam currently set to zero. And note that we don’t need to reference the value of the spam column from the urls table, we’re already testing that it’s equal to 1, so we can use a literal 1 in the assignment of the value to the tweets.spam column. Also note that we are doing an INNER JOIN (rather than a LEFT OUTER JOIN), so, again, we will only be updating rows that will be assigned a value of 1.