<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:admin="http://webns.net/mvcb/"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
	<channel> 

	<title>Comments on: A SQL Join</title>
	<link>http://ask.metafilter.com/34505/A-SQL-Join/</link>
	<description>Comments on Ask MetaFilter post A SQL Join</description>
	<pubDate>Thu, 16 Mar 2006 15:05:49 -0800</pubDate>
	<lastBuildDate>Thu, 16 Mar 2006 15:05:49 -0800</lastBuildDate>
	<language>en-us</language>
	<docs>http://blogs.law.harvard.edu/tech/rss</docs>
	<ttl>60</ttl>

	<item>
		<title>Question: A SQL Join</title>
		<link>http://ask.metafilter.com/34505/A-SQL-Join</link>	
		<description>How do I manage this SQL join? &lt;br /&gt;&lt;br /&gt; I have two tables: USERS and LOGGED_IN_USERS created as follows (non-relevant columns removed)&lt;br&gt;
	&lt;br&gt;
CREATE TABLE Users&lt;br&gt;
(&lt;br&gt;
   user_id        INTEGER AUTO_INCREMENT PRIMARY KEY,&lt;br&gt;
   remember  TINYINT(1) NOT NULL&lt;br&gt;
)&lt;br&gt;
&lt;br&gt;
CREATE TABLE LoggedInUsers&lt;br&gt;
(&lt;br&gt;
   user_id          INTEGER PRIMARY KEY,&lt;br&gt;
   session_id      VARCHAR(255) UNIQUE NOT NULL,&lt;br&gt;
   last_access    DATETIME NOT NULL,&lt;br&gt;
&lt;br&gt;
FOREIGN KEY (user_id) REFERENCES Users (user_id)&lt;br&gt;
)&lt;br&gt;
&lt;br&gt;
What I want to do is write a query that deletes rows from the LoggedInUsers whose last_access is more than some variable $interval hours old and whose value in the Users.remember column is 0.&lt;br&gt;
&lt;br&gt;
This query works fine to delete old logins&lt;br&gt;
&lt;br&gt;
DELETE FROM LoggedInUsers&lt;br&gt;
WHERE last_access &amp;lt;= DATE_SUB(NOW(), INTERVAL $interval)&lt;br&gt;
&lt;br&gt;
Now I want to add the condition that the DELETE only happen if the &apos;remember&apos; value in table Users is 0 for the row with the same user_id.</description>
		<guid isPermaLink="false">post:ask.metafilter.com,2006:site.34505</guid>
		<pubDate>Thu, 16 Mar 2006 15:03:34 -0800</pubDate>
		<dc:creator>nonmyopicdave</dc:creator>
		
			<category>SQL</category>
		
	</item> <item>
		<title>By: Bezbozhnik</title>
		<link>http://ask.metafilter.com/34505/A-SQL-Join#537984</link>	
		<description>Which database are you using? MySQL, SQL Server, Oracle, etc?</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.34505-537984</guid>
		<pubDate>Thu, 16 Mar 2006 15:05:49 -0800</pubDate>
		<dc:creator>Bezbozhnik</dc:creator>
	</item><item>
		<title>By: valkyrie</title>
		<link>http://ask.metafilter.com/34505/A-SQL-Join#537989</link>	
		<description>You could use a subselect, i.e.:&lt;br&gt;
&lt;br&gt;
DELETE FROM LoggedInUsers&lt;br&gt;
WHERE last_access &lt; date_sub(now(), interval $interval)br&gt;
AND user_id in&lt;br&gt;
(select user_id from Users where remember = 0)&lt;/&gt;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.34505-537989</guid>
		<pubDate>Thu, 16 Mar 2006 15:10:58 -0800</pubDate>
		<dc:creator>valkyrie</dc:creator>
	</item><item>
		<title>By: nonmyopicdave</title>
		<link>http://ask.metafilter.com/34505/A-SQL-Join#537994</link>	
		<description>The database is MySQL.&lt;br&gt;
&lt;br&gt;
Thanks valkyrie.  I&apos;ll give it a shot (sans &quot;br&amp;gt;&quot;) and report back.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.34505-537994</guid>
		<pubDate>Thu, 16 Mar 2006 15:17:03 -0800</pubDate>
		<dc:creator>nonmyopicdave</dc:creator>
	</item><item>
		<title>By: Paris Hilton</title>
		<link>http://ask.metafilter.com/34505/A-SQL-Join#538034</link>	
		<description>Hmm, I don&apos;t think MySQL supports subselects at this time, I know it didn&apos;t a couple years ago. &lt;br&gt;
&lt;br&gt;
Why not just be lazy and add a &apos;remember&apos; column to logged_on_users?&lt;br&gt;
&lt;br&gt;
It will actually make the query run much faster, because it will only need to lookup one table during the query.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.34505-538034</guid>
		<pubDate>Thu, 16 Mar 2006 15:59:57 -0800</pubDate>
		<dc:creator>Paris Hilton</dc:creator>
	</item><item>
		<title>By: Paris Hilton</title>
		<link>http://ask.metafilter.com/34505/A-SQL-Join#538036</link>	
		<description>In fact, why not just be &lt;b&gt;really&lt;/b&gt; lazy and add those values to the users table? then it would just be &lt;br&gt;
&lt;br&gt;
update users set logged_on = 0 where last_access &lt; now() - @intervalbr&gt;
&lt;br&gt;
or whatever in MySQL&lt;/&gt;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.34505-538036</guid>
		<pubDate>Thu, 16 Mar 2006 16:02:02 -0800</pubDate>
		<dc:creator>Paris Hilton</dc:creator>
	</item><item>
		<title>By: Paris Hilton</title>
		<link>http://ask.metafilter.com/34505/A-SQL-Join#538037</link>	
		<description>huh, how did that br get there, weird.&lt;br&gt;
&lt;br&gt;
update users set logged_on = 0 where last_access &amp;lt; now() - @interval&lt;br&gt;
&lt;br&gt;
ah, it was my less-than sign.  heh.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.34505-538037</guid>
		<pubDate>Thu, 16 Mar 2006 16:02:53 -0800</pubDate>
		<dc:creator>Paris Hilton</dc:creator>
	</item><item>
		<title>By: fishfucker</title>
		<link>http://ask.metafilter.com/34505/A-SQL-Join#538038</link>	
		<description>MySQL supports subselects as of 4.1.*</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.34505-538038</guid>
		<pubDate>Thu, 16 Mar 2006 16:03:09 -0800</pubDate>
		<dc:creator>fishfucker</dc:creator>
	</item><item>
		<title>By: fishfucker</title>
		<link>http://ask.metafilter.com/34505/A-SQL-Join#538045</link>	
		<description>&lt;br&gt;
If you&apos;re working with 4.0 this should work: &lt;br&gt;
&lt;br&gt;
DELETE FROM LoggedInUsers USING LoggedInUsers, Users WHERE last_access &lt; date_sub(now(), interval $interval) and loggedinusers.user_id=Users.user_id and users.remember=0 br&gt;
&lt;br&gt;
I&apos;m a mysql neophyte though, so I&apos;d recommend testing this on some made up tables.&lt;/&gt;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.34505-538045</guid>
		<pubDate>Thu, 16 Mar 2006 16:09:45 -0800</pubDate>
		<dc:creator>fishfucker</dc:creator>
	</item><item>
		<title>By: nonmyopicdave</title>
		<link>http://ask.metafilter.com/34505/A-SQL-Join#538079</link>	
		<description>You know Paris, that might be a better solution-- add the &apos;remember&apos; column to LoggedInUsers. Because it&apos;s not a user who  wants their login to not expire, but rather a user&apos;s specific session.&lt;br&gt;
&lt;br&gt;
Genius!&lt;br&gt;
&lt;br&gt;
Incidentally, my database connection is down, so I can&apos;t test the subselect yet.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.34505-538079</guid>
		<pubDate>Thu, 16 Mar 2006 16:49:08 -0800</pubDate>
		<dc:creator>nonmyopicdave</dc:creator>
	</item><item>
		<title>By: karen</title>
		<link>http://ask.metafilter.com/34505/A-SQL-Join#538081</link>	
		<description>the syntax is:&lt;br&gt;
&lt;br&gt;
delete LoggedInUsers from LoggedInUsers l, Users u where l.last_access &lt; date_sub(now(), interval $interval) and l.user_id=u.user_id and u.remember=0&lt;br&gt;
&lt;br&gt;
that should work&lt;/&gt;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.34505-538081</guid>
		<pubDate>Thu, 16 Mar 2006 16:50:41 -0800</pubDate>
		<dc:creator>karen</dc:creator>
	</item><item>
		<title>By: karen</title>
		<link>http://ask.metafilter.com/34505/A-SQL-Join#538082</link>	
		<description>btw, you do not need a subselect since this is a straight join. you just want to make sure it only deletes them from LoggedInUsers and the above syntax I gave should work. I use it all over.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.34505-538082</guid>
		<pubDate>Thu, 16 Mar 2006 16:52:16 -0800</pubDate>
		<dc:creator>karen</dc:creator>
	</item><item>
		<title>By: ChasFile</title>
		<link>http://ask.metafilter.com/34505/A-SQL-Join#538175</link>	
		<description>What about:&lt;br&gt;
&lt;br&gt;
CREATE TABLE Users&lt;br&gt;
(&lt;br&gt;
user_id INTEGER AUTO_INCREMENT PRIMARY KEY,&lt;br&gt;
remember TINYINT(1) NOT NULL,&lt;br&gt;
logged_in TINYINT(1) NOT NULL,&lt;br&gt;
session_id VARCHAR(255) UNIQUE NOT NULL,&lt;br&gt;
last_access DATETIME NOT NULL,&lt;br&gt;
)&lt;br&gt;
&lt;br&gt;
UPDATE Users SET logged_in = 0 WHERE last_access &lt; date_sub(now(), interval $interval) and remember=0;&lt;br&gt;
&lt;br&gt;
I think your way is probably better in terms of normalization and disk usage, but mine is better in terms of speed and resources usage.  Like anything else, its a trade-off.&lt;/&gt;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.34505-538175</guid>
		<pubDate>Thu, 16 Mar 2006 19:35:48 -0800</pubDate>
		<dc:creator>ChasFile</dc:creator>
	</item><item>
		<title>By: nonmyopicdave</title>
		<link>http://ask.metafilter.com/34505/A-SQL-Join#538198</link>	
		<description>Thanks karen. I decided to stick the &apos;remember&apos; col in the LoggedInUsers table, so I won&apos;t need the join. &lt;br&gt;
&lt;br&gt;
ChasFile: Yeah, it seems better to me to separate the User info, which may be a large table, with the current session info, which will be smaller. And that LoggedInUsers gets hit more.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.34505-538198</guid>
		<pubDate>Thu, 16 Mar 2006 20:31:56 -0800</pubDate>
		<dc:creator>nonmyopicdave</dc:creator>
	</item>
	</channel>
</rss>
