Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Select Query Help

 
   Database Help (Home) -> mySQL RSS
Next:  subquery returned more than one value  
Author Message
James Baker

External


Since: Sep 01, 2010
Posts: 2



(Msg. 1) Posted: Wed Sep 01, 2010 1:25 pm
Post subject: Select Query Help
Archived from groups: comp>databases>mysql (more info?)

I'm struggling - with my limited knowledge of SQL - to write a query
that does what I want it to, and I was hoping that someone here may be
able to help out.

I have a table with two columns, user1 and user2, which store user
ID's. For example:

user1 | user2
-----------------------------
1 | 4
1 | 2
1 | 3
3 | 2
3 | 1
5 | 1

What I want to do is select all the rows for which, say, user2 is 1,
but where user1 is such that there isn't a reverse combination. So
above, the only row that should come back is 5/1 as for the row 3/1
there is also a row 1/3.

I know that explanation isn't perhaps the clearest, but hopefully you
get the idea.

Thanks in advance,
James Baker

 >> Stay informed about: Select Query Help 
Back to top
Login to vote
Lennart Jonsson

External


Since: Apr 26, 2010
Posts: 22



(Msg. 2) Posted: Wed Sep 01, 2010 3:25 pm
Post subject: Re: Select Query Help [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 2010-09-01 18:30, James Baker wrote:
> I'm struggling - with my limited knowledge of SQL - to write a query
> that does what I want it to, and I was hoping that someone here may be
> able to help out.
>
> I have a table with two columns, user1 and user2, which store user
> ID's. For example:
>
> user1 | user2
> -----------------------------
> 1 | 4
> 1 | 2
> 1 | 3
> 3 | 2
> 3 | 1
> 5 | 1
>
> What I want to do is select all the rows for which, say, user2 is 1,
> but where user1 is such that there isn't a reverse combination. So
> above, the only row that should come back is 5/1 as for the row 3/1
> there is also a row 1/3.
>
> I know that explanation isn't perhaps the clearest, but hopefully you
> get the idea.
>

select t1.user1, t1.user2
from t t1
where t1.user2 = 1
and not exists (
select 1 from t t2
where (t1.user1, t1.user2) = (t2.user2, t2.user1)
);

/Lennart

 >> Stay informed about: Select Query Help 
Back to top
Login to vote
Robert Hairgrove

External


Since: Nov 19, 2009
Posts: 6



(Msg. 3) Posted: Wed Sep 01, 2010 3:25 pm
Post subject: Re: Select Query Help [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

James Baker wrote:
> I'm struggling - with my limited knowledge of SQL - to write a query
> that does what I want it to, and I was hoping that someone here may be
> able to help out.
>
> I have a table with two columns, user1 and user2, which store user ID's.
> For example:
>
> user1 | user2
> -----------------------------
> 1 | 4
> 1 | 2
> 1 | 3
> 3 | 2
> 3 | 1
> 5 | 1
>
> What I want to do is select all the rows for which, say, user2 is 1, but
> where user1 is such that there isn't a reverse combination. So above,
> the only row that should come back is 5/1 as for the row 3/1 there is
> also a row 1/3.
>
> I know that explanation isn't perhaps the clearest, but hopefully you
> get the idea.
>
> Thanks in advance,
> James Baker

You could try this (untested, but very straightforward):

SELECT T1.* FROM the_table T1
WHERE NOT EXISTS (SELECT * FROM the_table T2
WHERE (T1.user1=T2.user2)
AND (T1.user2=T1.user1));

I assume that there are no rows where user1=user2 in the table ... but
perhaps this is assuming too much? Smile
 >> Stay informed about: Select Query Help 
Back to top
Login to vote
Robert Hairgrove

External


Since: Nov 19, 2009
Posts: 6



(Msg. 4) Posted: Wed Sep 01, 2010 3:25 pm
Post subject: Re: Select Query Help [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Robert Hairgrove wrote:
> SELECT T1.* FROM the_table T1
> WHERE NOT EXISTS (SELECT * FROM the_table T2
> WHERE (T1.user1=T2.user2)
> AND (T1.user2=T1.user1));

Sorry, a typo ... this should read:

SELECT T1.* FROM the_table T1
WHERE NOT EXISTS (SELECT * FROM the_table T2
WHERE (T1.user1=T2.user2)
AND (T1.user2=T2.user1));
^^^
 >> Stay informed about: Select Query Help 
Back to top
Login to vote
James Baker

External


Since: Sep 01, 2010
Posts: 2



(Msg. 5) Posted: Thu Sep 02, 2010 11:25 am
Post subject: Re: Select Query Help [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 2010-09-01 19:57:17 +0100, Robert Hairgrove said:

> Robert Hairgrove wrote:
>> SELECT T1.* FROM the_table T1
>> WHERE NOT EXISTS (SELECT * FROM the_table T2
>> WHERE (T1.user1=T2.user2)
>> AND (T1.user2=T1.user1));
>
> Sorry, a typo ... this should read:
>
> SELECT T1.* FROM the_table T1
> WHERE NOT EXISTS (SELECT * FROM the_table T2
> WHERE (T1.user1=T2.user2)
> AND (T1.user2=T2.user1));
> ^^^

Thanks, that worked perfectly. I didn't realise you could nest SELECT
statements like that.

James
 >> Stay informed about: Select Query Help 
Back to top
Login to vote
The Natural Philosopher

External


Since: Oct 01, 2009
Posts: 27



(Msg. 6) Posted: Thu Sep 02, 2010 11:25 am
Post subject: Re: Select Query Help [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

James Baker wrote:
> On 2010-09-01 19:57:17 +0100, Robert Hairgrove said:
>
>> Robert Hairgrove wrote:
>>> SELECT T1.* FROM the_table T1
>>> WHERE NOT EXISTS (SELECT * FROM the_table T2
>>> WHERE (T1.user1=T2.user2)
>>> AND (T1.user2=T1.user1));
>>
>> Sorry, a typo ... this should read:
>>
>> SELECT T1.* FROM the_table T1
>> WHERE NOT EXISTS (SELECT * FROM the_table T2
>> WHERE (T1.user1=T2.user2)
>> AND (T1.user2=T2.user1));
>> ^^^
>
> Thanks, that worked perfectly. I didn't realise you could nest SELECT
> statements like that.
>

yes, its a nice feature, but beware the search and memory time and space
needed to do this.

Indices on the main search fields will help enormously if you run into
speed problems. But you can't index a subqueries *result*!

> James
>
 >> Stay informed about: Select Query Help 
Back to top
Login to vote
Robert Hairgrove

External


Since: Nov 19, 2009
Posts: 6



(Msg. 7) Posted: Thu Sep 02, 2010 1:25 pm
Post subject: Re: Select Query Help [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

The Natural Philosopher wrote:
> James Baker wrote:
>> On 2010-09-01 19:57:17 +0100, Robert Hairgrove said:
>>> Robert Hairgrove wrote:
>>>> SELECT T1.* FROM the_table T1
>>>> WHERE NOT EXISTS (SELECT * FROM the_table T2
>>>> WHERE (T1.user1=T2.user2)
>>>> AND (T1.user2=T1.user1));
>>>
>>> Sorry, a typo ... this should read:
>>>
>>> SELECT T1.* FROM the_table T1
>>> WHERE NOT EXISTS (SELECT * FROM the_table T2
>>> WHERE (T1.user1=T2.user2)
>>> AND (T1.user2=T2.user1));
>>> ^^^
>>
>> Thanks, that worked perfectly. I didn't realise you could nest SELECT
>> statements like that.
>>
>
> yes, its a nice feature, but beware the search and memory time and space
> needed to do this.
>
> Indices on the main search fields will help enormously if you run into
> speed problems. But you can't index a subqueries *result*!

And, as someone once said (was it Don Knuth??): "Beware of premature
optimization"...

This kind of query is probably done only once, and if there are indices
on user1 and user2, they will (most likely) also be used in the subquery.
 >> Stay informed about: Select Query Help 
Back to top
Login to vote
Lennart Jonsson

External


Since: Apr 26, 2010
Posts: 22



(Msg. 8) Posted: Thu Sep 02, 2010 1:25 pm
Post subject: Re: Select Query Help [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 2010-09-02 18:43, Robert Hairgrove wrote:
[...]
>
> And, as someone once said (was it Don Knuth??): "Beware of premature
> optimization"...

I thought it was Dijkstra who said:

"Premature optimization is the root of all evil"

but after reading:


http://hans.gerwitz.com/2004/08/12/premature-optimization-is-the-root-...all-evi

I'm not so sure who sait what Smile


/Lennart
 >> Stay informed about: Select Query Help 
Back to top
Login to vote
Captain Paralytic

External


Since: Jan 14, 2008
Posts: 245



(Msg. 9) Posted: Fri Sep 03, 2010 4:11 am
Post subject: Re: Select Query Help [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 2 Sep, 15:58, The Natural Philosopher wrote:
> Indices on the main search fields will help enormously if you run into
> speed problems. But you can't index a subqueries *result*!

Surely this has no relevance to an "EXISTS" subquery, since there is
no "subqueries[SIC]" (see note) result to index.

Note: should be" subquery's"
 >> Stay informed about: Select Query Help 
Back to top
Login to vote
strawberry

External


Since: Feb 05, 2008
Posts: 11



(Msg. 10) Posted: Tue Sep 07, 2010 2:28 am
Post subject: Re: Select Query Help [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Sep 3, 12:11 pm, Captain Paralytic wrote:
> On 2 Sep, 15:58, The Natural Philosopher wrote:
>
> > Indices on the main search fields will help enormously if you run into
> > speed problems. But you can't index a subqueries *result*!
>
> Surely this has no relevance to an "EXISTS" subquery, since there is
> no "subqueries[SIC]" (see note) result to index.
>
> Note: should be" subquery's"

Am I missing something here?

SELECT t1.* FROM two_users t1 LEFT JOIN two_users t2 ON
(t2.user2,t2.user1) = (t1.user1,t1.user2) WHERE t1.user2 = 1 AND
t2.user1 IS NULL;
 >> Stay informed about: Select Query Help 
Back to top
Login to vote
strawberry

External


Since: Feb 05, 2008
Posts: 11



(Msg. 11) Posted: Tue Sep 07, 2010 3:34 am
Post subject: Re: Select Query Help [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Sep 7, 10:28 am, strawberry wrote:
> On Sep 3, 12:11 pm, Captain Paralytic wrote:
>
> > On 2 Sep, 15:58, The Natural Philosopher wrote:
>
> > > Indices on the main search fields will help enormously if you run into
> > > speed problems. But you can't index a subqueries *result*!
>
> > Surely this has no relevance to an "EXISTS" subquery, since there is
> > no "subqueries[SIC]" (see note) result to index.
>
> > Note: should be" subquery's"
>
> Am I missing something here?
>
> SELECT t1.* FROM two_users t1 LEFT JOIN two_users t2 ON
> (t2.user2,t2.user1) = (t1.user1,t1.user2) WHERE t1.user2 = 1 AND
> t2.user1 IS NULL;

And it should of course be ' the love of premature optimisation is the
root of all evil'
 >> Stay informed about: Select Query Help 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
select query on latin1 or utf8 column: which is faster? - Assume you have two varchar (or Text) columns named L and U which are identical except that the charset for L is latin1 and the charset for U is utf8. All the records in L and U are identical in terms of content, consisting of only 7 bit ASCII characters...

Select more than just * - Is it possible to run a query like this: SELECT *, DATEDIFF(CURDATE(), `updated`) AS `daysSinceUpdate` FROM `Admin` Or should I let PHP calculate the days since update?

select with regexp - i have a column "path" with values eg. +---------------+ | path | +---------------+ |/path/path/path| +---------------+ |/path/path | +---------------+ |/path | +---------------+ i want to select the row where there are ...

Random select - Hi, When I do a select with a limit I get always the same records. Usually this is the desidered effect. But now I want to do a random select. So every time I do a select I should get a different set of results. What is the best way to achieve this..

Count lines in select - I have a table t1: id int unique added timestamp and second table t2: eid int id int unique t2.id are IDs from t1. t2.eid with the same value can be found in t2 more than for one entry but they will all have different IDs. I want to: ..
   Database Help (Home) -> mySQL All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]