Professional Web Applications Themes

Complicated UNION query - MySQL

Hello all I need to make a UNION query from at least 2 identical tables, and I have to make a JOIN query within each part of the query I want to do something like (doesn't work) (SELECT some fields from table1 and table2 FROM (table1 t1, table2 t2) WHERE some conditions) UNION (SELECT some fields from table1 and table2 FROM (table1 t1, table2 t2) WHERE some conditions) GROUP BY one field from table 1 ORDER BY some fields from table1 and table2 LIMIT 0, 10 In SELECT I use some MIN(), MAX() ans SUM that's why I used GROUP ...

  1. #1

    Default Complicated UNION query

    Hello all

    I need to make a UNION query from at least 2 identical tables, and I have to
    make a JOIN query within each part of the query

    I want to do something like (doesn't work)

    (SELECT some fields from table1 and table2 FROM (table1 t1, table2 t2) WHERE
    some conditions)
    UNION
    (SELECT some fields from table1 and table2 FROM (table1 t1, table2 t2) WHERE
    some conditions)
    GROUP BY one field from table 1
    ORDER BY some fields from table1 and table2
    LIMIT 0, 10

    In SELECT I use some MIN(), MAX() ans SUM that's why I used GROUP BY

    Can somebody help me please



    JalaL Guest

  2. #2

    Default Re: Complicated UNION query

    On 21 Mar, 01:49, "JalaL" <com> wrote: 

    "doesn't work"!!!

    Very helpful!

    What about it DOESN'T WORK!!!!!

    Is there a syntax error?
    Does it produce out put but not what you want?

    Help us to help you!

    Captain Guest

  3. #3

    Default Re: Complicated UNION query


    "Captain Paralytic" <com> a écrit dans le message de
    news: googlegroups.com... 
    >
    > "doesn't work"!!!
    >
    > Very helpful!
    >
    > What about it DOESN'T WORK!!!!!
    >
    > Is there a syntax error?
    > Does it produce out put but not what you want?
    >
    > Help us to help you!
    >[/ref]

    Oups, sorry... The query works only if I remove GROUP BY and ORDER BY and it
    takes very long time to execute, I have a very large table that's why I
    wanted to split it into small identical tables. I think mysql doesn't
    support joining tables in union.

    I think creating a temporary MERGE table each time works faster.


    JalaL Guest

  4. #4

    Default Re: Complicated UNION query

    On 21 Mar, 14:48, "JalaL" <com> wrote: [/ref]
    > [/ref]
    > [/ref]
    > [/ref]
    > [/ref]
    > [/ref]





    >
    > Oups, sorry... The query works only if I remove GROUP BY and ORDER BY andit
    > takes very long time to execute, I have a very large table that's why I
    > wanted to split it into small identical tables. I think mysql doesn't
    > support joining tables in union.
    >
    > I think creating a temporary MERGE table each time works faster.- Hide quoted text -
    >
    > - Show quoted text -[/ref]

    Sheesh, so now you tell us when it works, but you STILL haven't told
    us in what way it fails when it doesn't work!

    You say that "I think mysql doesn't support joining tables in union."
    but there is no JOIN in your query.

    You have made some statements like: "I need to make a UNION query from
    at least 2 identical tables", but haven't explained why you need to do
    this.

    You haven't even shown us a real query!

    If you want us to help you, you really must be more specific!

    Care to try again?

    Captain Guest

  5. #5

    Default Re: Complicated UNION query

    The GROUP BY is based on the subselect not the full select so each SELECT
    statement in the UNION needs its own GROUP BY.

    CJP

    On Wed, 21 Mar 2007 15:48:42 +0100, JalaL wrote:
     
    >>
    >> "doesn't work"!!!
    >>
    >> Very helpful!
    >>
    >> What about it DOESN'T WORK!!!!!
    >>
    >> Is there a syntax error?
    >> Does it produce out put but not what you want?
    >>
    >> Help us to help you!
    >>[/ref]
    >
    > Oups, sorry... The query works only if I remove GROUP BY and ORDER BY and it
    > takes very long time to execute, I have a very large table that's why I
    > wanted to split it into small identical tables. I think mysql doesn't
    > support joining tables in union.
    >
    > I think creating a temporary MERGE table each time works faster.[/ref]

    Christopher Guest

Similar Threads

  1. Complicated SQL query
    By pblecha in forum Coldfusion Database Access
    Replies: 6
    Last Post: April 4th, 05:56 PM
  2. complicated mysql query - please help :)
    By joeriviera@gmail.com in forum MySQL
    Replies: 5
    Last Post: March 17th, 04:17 PM
  3. complicated query - please help
    By Ralph in forum MySQL
    Replies: 7
    Last Post: October 8th, 09:58 PM
  4. UNION query
    By John in forum MySQL
    Replies: 3
    Last Post: October 5th, 05:52 PM
  5. Requesting help with complicated query
    By Fox in forum Microsoft SQL / MS SQL Server
    Replies: 2
    Last Post: July 7th, 10:46 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139