comparing two tables

This is a discussion on comparing two tables within the Database Discussions forums in Database and Unix Discussions category; Captain Paralytic wrote: > Cleverbum wrote: > > > addinall wrote: > > > Cleverbum wrote: > > > > Forgive me if you think the subject is wrong, but I can't quite think > > > > how to describe my question. > > > > I just deleted some rows of a table and need to see if there were any > > > > values in another table linked to the rows I deleted. > > > > I'm looking for a query that would perform: > > > > Show all records from table1 where the value in column `album` is not > > > > contained in the any of the rows in column `uid` of table2 > > > > If that makes no sense let me know and I'll ...

Go Back   Database Forum > Database and Unix Discussions > Database Discussions

Database Forums

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #31  
Old 01-16-2007, 12:13 PM
Default Re: comparing two tables


Captain Paralytic wrote:
> Cleverbum wrote:
>
> > addinall wrote:
> > > Cleverbum wrote:
> > > > Forgive me if you think the subject is wrong, but I can't quite think
> > > > how to describe my question.
> > > > I just deleted some rows of a table and need to see if there were any
> > > > values in another table linked to the rows I deleted.
> > > > I'm looking for a query that would perform:
> > > > Show all records from table1 where the value in column `album` is not
> > > > contained in the any of the rows in column `uid` of table2
> > > > If that makes no sense let me know and I'll try to re-word it and write
> > > > an example.
> > >
> > > select album from table_1
> > > union
> > > select uid from table_2
> > > where
> > > uid not in
> > > (select album from table_1)
> > >
> > > I think that seems to be what you require.
> > >
> > > If not, sorry! I mis-understood, or I'm just plain
> > > stupid!
> > >
> > > Mark Addinall.

> >
> > That seems closer, what actually seems to do the trick is:
> >
> > SELECT *
> > FROM personal_images
> > WHERE album NOT
> > IN (
> >
> > SELECT uid
> > FROM personal_albums
> > )

>
> This query will produce exactly the asme results as Strawberry's LEFT
> JOIN compare with NULL query.
>
> The difference is that the LEFT join is much much much more efficient,
> especially if there is an appropriate index.
>
> See http://dev.mysql.com/doc/refman/5.0/...ubqueries.html


I'll bare that in mind, I couldn't understand the left join one as well
so found it harder to modify when it came up with an error first time.
Thanks for all your help.
Martin.

Reply With Quote
Reply


Thread Tools
Display Modes



All times are GMT -4. The time now is 07:34 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Integrated by bbpixel2008 :: jvbPlugin R1013.368.1

Search Engine Friendly URLs by vBSEO 3.1.0
vB Ad Management by =RedTyger=
In an effort to better serve ads to our visitors, cookies are used on Mydatabasesupport.com. For more information, check out our Privacy Policy.