Difficult query

This is a discussion on Difficult query within the MySQL Database forums, part of the Database Forums category; Hello, First, excuses for my bad english. I have a table: mysql> select * from test; +----+---------+---------+------+ | id | column1 | column2 | text | +----+---------+---------+------+ | ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-19-2007
eebieeebie@hotmail.com
 
Posts: n/a
Default Difficult query

Hello,

First, excuses for my bad english.

I have a table:

mysql> select * from test;
+----+---------+---------+------+
| id | column1 | column2 | text |
+----+---------+---------+------+
| 1 | 1 | 1 | qwe |
| 2 | 2 | 1 | asd |
| 3 | 3 | 1 | zxc |
| 4 | 4 | 1 | zaq |
| 5 | 1 | 2 | xsw |
| 6 | 2 | 2 | cde |
| 7 | 3 | 2 | ewq |
| 8 | 4 | 2 | dsa |
| 9 | 1 | 3 | cxz |
| 10 | 2 | 3 | esz |
| 11 | 3 | 3 | csq |
| 12 | 4 | 2 | zse |
+----+---------+---------+------+

I want to select all the values from column1 one time and the maximum
value of column2 that correspond with that and also the value of the
column text that is on the same row.

I have the next solution but it isn't very nice:

mysql> CREATE TABLE tmp SELECT column1, MAX(column2) AS column2 FROM
test GROUP BY column1;

mysql> select test.column1, test.column2, text FROM test, tmp WHERE
test.column1 = tmp.column1 AND test.column2 = tmp.column2;
+---------+---------+------+
| column1 | column2 | text |
+---------+---------+------+
| 1 | 3 | cxz |
| 2 | 3 | esz |
| 3 | 3 | csq |
| 4 | 2 | dsa |
| 4 | 2 | zse |
+---------+---------+------+

mysql> DROP table tmp;

Does anybody know a nicer way to that? I tried joins and a lot of
other things but this is the only way I got it working.

Again, excuses for my bad engisch (I'm from holland)

--
EeBie

Reply With Quote
  #2 (permalink)  
Old 04-19-2007
Captain Paralytic
 
Posts: n/a
Default Re: Difficult query

On Apr 19, 10:52 am, eebieee...@hotmail.com wrote:
> Hello,
>
> First, excuses for my bad english.
>
> I have a table:
>
> mysql> select * from test;
> +----+---------+---------+------+
> | id | column1 | column2 | text |
> +----+---------+---------+------+
> | 1 | 1 | 1 | qwe |
> | 2 | 2 | 1 | asd |
> | 3 | 3 | 1 | zxc |
> | 4 | 4 | 1 | zaq |
> | 5 | 1 | 2 | xsw |
> | 6 | 2 | 2 | cde |
> | 7 | 3 | 2 | ewq |
> | 8 | 4 | 2 | dsa |
> | 9 | 1 | 3 | cxz |
> | 10 | 2 | 3 | esz |
> | 11 | 3 | 3 | csq |
> | 12 | 4 | 2 | zse |
> +----+---------+---------+------+
>
> I want to select all the values from column1 one time and the maximum
> value of column2 that correspond with that and also the value of the
> column text that is on the same row.
>
> I have the next solution but it isn't very nice:
>
> mysql> CREATE TABLE tmp SELECT column1, MAX(column2) AS column2 FROM
> test GROUP BY column1;
>
> mysql> select test.column1, test.column2, text FROM test, tmp WHERE
> test.column1 = tmp.column1 AND test.column2 = tmp.column2;
> +---------+---------+------+
> | column1 | column2 | text |
> +---------+---------+------+
> | 1 | 3 | cxz |
> | 2 | 3 | esz |
> | 3 | 3 | csq |
> | 4 | 2 | dsa |
> | 4 | 2 | zse |
> +---------+---------+------+
>
> mysql> DROP table tmp;
>
> Does anybody know a nicer way to that? I tried joins and a lot of
> other things but this is the only way I got it working.
>
> Again, excuses for my bad engisch (I'm from holland)
>
> --
> EeBie


Your requirement says: "I want to select all the values from column1
one time", but your sample output has two entries for the column1
value "4". These contradict each other. Which do you actually want to
see?

Reply With Quote
  #3 (permalink)  
Old 04-19-2007
eebieeebie@hotmail.com
 
Posts: n/a
Default Re: Difficult query

On 19 apr, 12:01, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> On Apr 19, 10:52 am, eebieee...@hotmail.com wrote:
>
>
> Your requirement says: "I want to select all the values from column1
> one time", but your sample output has two entries for the column1
> value "4". These contradict each other. Which do you actually want to
> see?


You are right, I said it wrong. I only want the values where column2
is maximum , most of the time that gives one value but not all the
time.

--
EeBie

Reply With Quote
  #4 (permalink)  
Old 04-19-2007
strawberry
 
Posts: n/a
Default Re: Difficult query

On Apr 19, 11:01 am, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> On Apr 19, 10:52 am, eebieee...@hotmail.com wrote:
>
>
>
> > Hello,

>
> > First, excuses for my bad english.

>
> > I have a table:

>
> > mysql> select * from test;
> > +----+---------+---------+------+
> > | id | column1 | column2 | text |
> > +----+---------+---------+------+
> > | 1 | 1 | 1 | qwe |
> > | 2 | 2 | 1 | asd |
> > | 3 | 3 | 1 | zxc |
> > | 4 | 4 | 1 | zaq |
> > | 5 | 1 | 2 | xsw |
> > | 6 | 2 | 2 | cde |
> > | 7 | 3 | 2 | ewq |
> > | 8 | 4 | 2 | dsa |
> > | 9 | 1 | 3 | cxz |
> > | 10 | 2 | 3 | esz |
> > | 11 | 3 | 3 | csq |
> > | 12 | 4 | 2 | zse |
> > +----+---------+---------+------+

>
> > I want to select all the values from column1 one time and the maximum
> > value of column2 that correspond with that and also the value of the
> > column text that is on the same row.

>
> > I have the next solution but it isn't very nice:

>
> > mysql> CREATE TABLE tmp SELECT column1, MAX(column2) AS column2 FROM
> > test GROUP BY column1;

>
> > mysql> select test.column1, test.column2, text FROM test, tmp WHERE
> > test.column1 = tmp.column1 AND test.column2 = tmp.column2;
> > +---------+---------+------+
> > | column1 | column2 | text |
> > +---------+---------+------+
> > | 1 | 3 | cxz |
> > | 2 | 3 | esz |
> > | 3 | 3 | csq |
> > | 4 | 2 | dsa |
> > | 4 | 2 | zse |
> > +---------+---------+------+

>
> > mysql> DROP table tmp;

>
> > Does anybody know a nicer way to that? I tried joins and a lot of
> > other things but this is the only way I got it working.

>
> > Again, excuses for my bad engisch (I'm from holland)

>
> > --
> > EeBie

>
> Your requirement says: "I want to select all the values from column1
> one time", but your sample output has two entries for the column1
> value "4". These contradict each other. Which do you actually want to
> see?


I'm assuming EeBie wants to see all results holding the max value for
each group:

SELECT t1. *
FROM test t1
LEFT JOIN test t2 ON t2.column1 = t1.column1
AND t1.column2 < t2.column2
WHERE t2.column2 IS NULL


BTW EeBie, don't apologise. Your English is almost perfect:

correspond -> corresponds
next -> following

Reply With Quote
  #5 (permalink)  
Old 04-19-2007
eebieeebie@hotmail.com
 
Posts: n/a
Default Re: Difficult query

On 19 apr, 12:29, strawberry <zac.ca...@gmail.com> wrote:
> On Apr 19, 11:01 am, Captain Paralytic <paul_laut...@yahoo.com> wrote:
>
>
>
> > On Apr 19, 10:52 am, eebieee...@hotmail.com wrote:

>
> > > Hello,

>
> > > First, excuses for my bad english.

>
> > > I have a table:

>
> > > mysql> select * from test;
> > > +----+---------+---------+------+
> > > | id | column1 | column2 | text |
> > > +----+---------+---------+------+
> > > | 1 | 1 | 1 | qwe |
> > > | 2 | 2 | 1 | asd |
> > > | 3 | 3 | 1 | zxc |
> > > | 4 | 4 | 1 | zaq |
> > > | 5 | 1 | 2 | xsw |
> > > | 6 | 2 | 2 | cde |
> > > | 7 | 3 | 2 | ewq |
> > > | 8 | 4 | 2 | dsa |
> > > | 9 | 1 | 3 | cxz |
> > > | 10 | 2 | 3 | esz |
> > > | 11 | 3 | 3 | csq |
> > > | 12 | 4 | 2 | zse |
> > > +----+---------+---------+------+

>
> > > I want to select all the values from column1 one time and the maximum
> > > value of column2 that correspond with that and also the value of the
> > > column text that is on the same row.

>
> > > I have the next solution but it isn't very nice:

>
> > > mysql> CREATE TABLE tmp SELECT column1, MAX(column2) AS column2 FROM
> > > test GROUP BY column1;

>
> > > mysql> select test.column1, test.column2, text FROM test, tmp WHERE
> > > test.column1 = tmp.column1 AND test.column2 = tmp.column2;
> > > +---------+---------+------+
> > > | column1 | column2 | text |
> > > +---------+---------+------+
> > > | 1 | 3 | cxz |
> > > | 2 | 3 | esz |
> > > | 3 | 3 | csq |
> > > | 4 | 2 | dsa |
> > > | 4 | 2 | zse |
> > > +---------+---------+------+

>
> > > mysql> DROP table tmp;

>
> > > Does anybody know a nicer way to that? I tried joins and a lot of
> > > other things but this is the only way I got it working.

>
> > > Again, excuses for my bad engisch (I'm from holland)

>
> > > --
> > > EeBie

>
> > Your requirement says: "I want to select all the values from column1
> > one time", but your sample output has two entries for the column1
> > value "4". These contradict each other. Which do you actually want to
> > see?

>
> I'm assuming EeBie wants to see all results holding the max value for
> each group:
>
> SELECT t1. *
> FROM test t1
> LEFT JOIN test t2 ON t2.column1 = t1.column1
> AND t1.column2 < t2.column2
> WHERE t2.column2 IS NULL


That works! I don't know exactly how but I try to figure it out.
Thank you.

> BTW EeBie, don't apologise. Your English is almost perfect:
>
> correspond -> corresponds
> next -> following


:-)

--
EeBie

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 04:35 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0