PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.databases.mysql > Is this a bug?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Is this a bug?

Réponse
 
LinkBack Outils de la discussion
Vieux 05/02/2008, 12h13   #1
Jeremy@thebunnyshed.co.uk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Is this a bug?

After spending the last hour tracking down a problem with my large
query, I have isolated this simple test case:

SELECT 1 AS Test1, 2 As Test2
UNION ALL
SELECT 2 As Test2, 1 AS Test1

Produces:

Test1 Test2
1 2
2 1

This looks very obviously wrong to me, but just wanted to check it's
not me misunderstanding some kind of usage rules with UNION or
something.

Cheers,
Jeremy
  Réponse avec citation
Vieux 05/02/2008, 12h32   #2
Pavel Lepin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is this a bug?


Jeremy@thebunnyshed.co.uk <Jeremy@thebunnyshed.co.uk> wrote
in
<e832a67b-d352-46e0-9e3f-7714f75b73f9@m34g2000hsf.googlegroups.com>:
> After spending the last hour tracking down a problem with
> my large query, I have isolated this simple test case:
>
> SELECT 1 AS Test1, 2 As Test2
> UNION ALL
> SELECT 2 As Test2, 1 AS Test1
>
> Produces:
>
> Test1 Test2
> 1 2
> 2 1
>
> This looks very obviously wrong to me, but just wanted to
> check it's not me misunderstanding some kind of usage
> rules with UNION or something.


11.2.7.3. UNION syntax. Read the second sentence. Then the
third sentence. Repeat until enlightened.

--
When all you have is a transformation engine, everything
looks like a tree.
  Réponse avec citation
Vieux 05/02/2008, 12h33   #3
Captain Paralytic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is this a bug?

On 5 Feb, 12:13, Jer...@thebunnyshed.co.uk wrote:
> After spending the last hour tracking down a problem with my large
> query, I have isolated this simple test case:
>
> SELECT 1 AS Test1, 2 As Test2
> UNION ALL
> SELECT 2 As Test2, 1 AS Test1
>
> Produces:
>
> Test1 Test2
> 1 2
> 2 1
>
> This looks very obviously wrong to me, but just wanted to check it's
> not me misunderstanding some kind of usage rules with UNION or
> something.
>
> Cheers,
> Jeremy


What amazes me here is that the excellenet explanation in the second
and third paragraphs of the manual section on UNION syntax did not
answer your question when you read it before bothering to post here???

http://dev.mysql.com/doc/refman/5.0/en/union.html
  Réponse avec citation
Vieux 05/02/2008, 12h36   #4
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is this a bug?

Jeremy@thebunnyshed.co.uk wrote:
> After spending the last hour tracking down a problem with my large
> query, I have isolated this simple test case:
>
> SELECT 1 AS Test1, 2 As Test2
> UNION ALL
> SELECT 2 As Test2, 1 AS Test1
>
> Produces:
>
> Test1 Test2
> 1 2
> 2 1
>
> This looks very obviously wrong to me, but just wanted to check it's
> not me misunderstanding some kind of usage rules with UNION or
> something.
>
> Cheers,
> Jeremy
>


That is correct. You are retrieving two columns - A and B, in that
order. And that is how both rows are displayed.

MySQL command line does show the names at the top of the column for your
convenience, and when using a UNION, those names come from the first
clause. But MySQL does not resequence the columns based on the names.
Those are there only for your convenience.

In a program, if you return the elements in an indexed (non-associative)
array, you will get as shown. But you retrieve them as an associative
array, [Test1] will contain 1 in both rows and [Test2] will contain 2 in
both rows.



--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  Réponse avec citation
Vieux 05/02/2008, 12h59   #5
Captain Paralytic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is this a bug?

On 5 Feb, 12:36, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> But you retrieve them as an associative
> array, [Test1] will contain 1 in both rows and [Test2] will contain 2 in
> both rows.

Hmmm, now based on what the manual says, this is not the behaviour I
would expect.
  Réponse avec citation
Vieux 05/02/2008, 13h02   #6
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is this a bug?

Captain Paralytic wrote:
> On 5 Feb, 12:36, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>> But you retrieve them as an associative
>> array, [Test1] will contain 1 in both rows and [Test2] will contain 2 in
>> both rows.

> Hmmm, now based on what the manual says, this is not the behaviour I
> would expect.
>


Now that I read it more closely, Paul, you're right. But it makes sense.

I shouldn't try to answer questions like this before my first cuppa joe. :-)

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  Réponse avec citation
Vieux 05/02/2008, 13h06   #7
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is this a bug?

On Tue, 05 Feb 2008 13:36:18 +0100, Jerry Stuckle
<jstucklex@attglobal.net> wrote:

> Jeremy@thebunnyshed.co.uk wrote:
>> After spending the last hour tracking down a problem with my large
>> query, I have isolated this simple test case:
>> SELECT 1 AS Test1, 2 As Test2
>> UNION ALL
>> SELECT 2 As Test2, 1 AS Test1
>> Produces:
>> Test1 Test2
>> 1 2
>> 2 1
>> This looks very obviously wrong to me, but just wanted to check it's
>> not me misunderstanding some kind of usage rules with UNION or
>> something.
>> Cheers,
>> Jeremy
>>

>
> That is correct. You are retrieving two columns - A and B, in that
> order. And that is how both rows are displayed.
>
> MySQL command line does show the names at the top of the column for your
> convenience, and when using a UNION, those names come from the first
> clause. But MySQL does not resequence the columns based on the names.
> Those are there only for your convenience.
>
> In a program, if you return the elements in an indexed (non-associative)
> array, you will get as shown. But you retrieve them as an associative
> array, [Test1] will contain 1 in both rows and [Test2] will contain 2 in
> both rows.


Hmm, not in PHP:
<?php
$sql = 'SELECT 1 AS Test1, 2 As Test2
UNION ALL
SELECT 2 As Test2, 1 AS Test1';
$result = mysql_query($sql);
while($row = mysql_fetch_row($result)) print_r($row);
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) print_r($row);
?>
Array
(
[0] => 1
[1] => 2
)
Array
(
[0] => 2
[1] => 1
)
Array
(
[Test1] => 1
[Test2] => 2
)
Array
(
[Test1] => 2
[Test2] => 1
)

Names for columns in the second argument just plainly don't count.
--
Rik Wasmus
  Réponse avec citation
Vieux 05/02/2008, 13h13   #8
Captain Paralytic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is this a bug?

On 5 Feb, 13:06, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
> On Tue, 05 Feb 2008 13:36:18 +0100, Jerry Stuckle
>
>
>
> <jstuck...@attglobal.net> wrote:
> > Jer...@thebunnyshed.co.uk wrote:
> >> After spending the last hour tracking down a problem with my large
> >> query, I have isolated this simple test case:
> >> SELECT 1 AS Test1, 2 As Test2
> >> UNION ALL
> >> SELECT 2 As Test2, 1 AS Test1
> >> Produces:
> >> Test1 Test2
> >> 1 2
> >> 2 1
> >> This looks very obviously wrong to me, but just wanted to check it's
> >> not me misunderstanding some kind of usage rules with UNION or
> >> something.
> >> Cheers,
> >> Jeremy

>
> > That is correct. You are retrieving two columns - A and B, in that
> > order. And that is how both rows are displayed.

>
> > MySQL command line does show the names at the top of the column for your
> > convenience, and when using a UNION, those names come from the first
> > clause. But MySQL does not resequence the columns based on the names.
> > Those are there only for your convenience.

>
> > In a program, if you return the elements in an indexed (non-associative)
> > array, you will get as shown. But you retrieve them as an associative
> > array, [Test1] will contain 1 in both rows and [Test2] will contain 2 in
> > both rows.

>
> Hmm, not in PHP:
> <?php
> $sql = 'SELECT 1 AS Test1, 2 As Test2
> UNION ALL
> SELECT 2 As Test2, 1 AS Test1';
> $result = mysql_query($sql);
> while($row = mysql_fetch_row($result)) print_r($row);
> $result = mysql_query($sql);
> while($row = mysql_fetch_assoc($result)) print_r($row);
> ?>
> Array
> (
> [0] => 1
> [1] => 2
> )
> Array
> (
> [0] => 2
> [1] => 1
> )
> Array
> (
> [Test1] => 1
> [Test2] => 2
> )
> Array
> (
> [Test1] => 2
> [Test2] => 1
> )
>
> Names for columns in the second argument just plainly don't count.
> --
> Rik Wasmus


That is how I read the manual.
  Réponse avec citation
Vieux 05/02/2008, 13h17   #9
Jeremy@thebunnyshed.co.uk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is this a bug?

> 11.2.7.3. UNION syntax. Read the second sentence. Then the
> third sentence. Repeat until enlightened.


D'oh!

Sorry guys. Exceedingly guilty of not RTFM.

<slinks away>

(Thanks anyway! :-)
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 14h35.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14477 seconds with 17 queries