Freelancers Network
 
skill list top cap
Homepage
Join the Freelancer's Network
Update your details
Find a freelancer
Post a project
Find a project
Projects Archive
Post a job
Find a job
Jobs Archive
See Dan's Pages
See Andy's Pages
Link to this site
Resources
Join/Leave Forum
Forum Messages
+Additions+ Adverts
Advertising
Contact Us
Subscribe to our newsletter - enter your email address and hit return
Freelancers.net is owned and operated by Andy Stowell and Dan Winchester
skill list end cap
guru web hostcom

Find me again on Freelancers.net

RE: FN-FORUM: MySQL Query Problems

date posted 31st January 2006 12:28

I agree ben

A join gives the opposite but you could run over the resulting array in php
or whatever and eliminate results, im a big believer in pushing as much work
to the parsing engine if the operation can be done that way as opposed to
using the DB,

ash

-----Original Message-----
From: [EMAIL REMOVED] [EMAIL REMOVED] On Behalf Of Ben Moxon
Sent: 31 January 2006 12:30
To: FN-FORUM / [EMAIL REMOVED]
Subject: Re: FN-FORUM: MySQL Query Problems


A join would be the right operation if you want to select everything in
Table1 with matching records from Table2 - so for example, if table1 has
an Id called table1_id and table 2 references it with a field called
t1_id (why do they not have the same name? to make it easier to follow
in this example...) you could join like this:

SELECT ta.*, tb.* FROM Table1 ta INNER JOIN Table2 tb ON
ta.table1_id=tb.t1_id

However, what you want is everything from table 2 that has no equivalent
in table 2, for which a join is not such an obvious choice.

If you're on mysql 3.23 you may even have to create a temporary table,
which would look a bit like this:

/* Create a copy of table 1 with an extra field to mark out if it is in
table 2 */
CREATE TEMPORARY TABLE temp TYPE=HEAP SELECT table1_id AS Id, 0 AS
inTab2 FROM Table1;
/* Update the temporary table to mark whether there is a crossover
with table2 - we can update on a join */
UPDATE temp tm INNER JOIN Table2 tb ON tm.id= tb.t1_id SET inTab2=1;
/* Delete all records where there is a crossover */
DELETE FROM temp WHERE inTab2=1;
/*Finally select the subset of Table1 that is still in the temporary
table.*/
SELECT ta.* FROM Table1 ta INNER JOIN temp tm ON ta.table1_id=tm.Id;

In php those queries have to be run separately.

It's convoluted but I don't recall many other ways to get "real"
database functionality in early versions of MySQL. Much easier if you
have a recent version, mind.

-ben


D D Glendinning wrote:

>Morning All!
>
>A few of you have helped me offlist, but its still not working, so
>thought I would post it, so what the rest of you think:
>
>Basically I have a table storing clients, and a table storing
>documents which have a 1 to many relationship with clients.
>
>I am trying to select "all clients from clients table who do
>not have a relationship with a given document" so I am using this
>query:
>
>$Query = "SELECT * FROM ABSclients WHERE clientID != '(SELECT clientID
>FROM ABSdocs_clients WHERE imageID='".$imageID"')' ORDER BY name";
>
>
>Which doesn't work. I was suggested that I use this (Thanks Ben!):
>
>SELECT * FROM ABSclients WHERE clientID NOT IN (SELECT clientID FROM
>ABSdocs_clients WHERE imageID=$imageID) ORDER BY name
>
>But not sure if its compatibility issues or something, but that
>doesn't work either.
>
>Is it a Join I am after? HELP!!!
>
>
>D
>
>
>


--
Freelancers, contractors earn more with Prosperity4
Call 0870 870 4414 or visit www.prosperity4.com
and benefit from Inland Revenue approved expenses today.

To advertise here: http://www.freelancers.net/advertising.html



Messages by Day
January 31st 2006
January 30th 2006
January 29th 2006
January 28th 2006
January 27th 2006
January 26th 2006
January 25th 2006
January 24th 2006
January 23rd 2006
January 22nd 2006
January 21st 2006
January 20th 2006
January 19th 2006
January 18th 2006
January 17th 2006
January 16th 2006
January 15th 2006
January 14th 2006
January 13th 2006
January 12th 2006
January 11th 2006
January 10th 2006
January 9th 2006
January 8th 2006
January 7th 2006
January 6th 2006
January 5th 2006
January 4th 2006
January 3rd 2006
January 2nd 2006
January 1st 2006


Messages by Month
December 2006
December 2006
October 2006
October 2006
August 2006
July 2006
July 2006
May 2006
May 2006
March 2006
March 2006
January 2006


Messages by Year
2008
2007
2006
2005
2004
2003
2002
2001
2000