If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > WHERE in UNION Syntax?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-06-03, 08:28
segovia6969 segovia6969 is offline
Registered User
 
Join Date: Nov 2003
Posts: 6
WHERE in UNION Syntax?

Hello,
I have a problem. Maybe someone can help me...:

Can I use a WHERE in a UNION Syntax?. Something like this:

$query = "(SELECT Name, Autor, Typ, Dateiname FROM dokumente)
UNION
(SELECT at.Arbeitsname, ut.Name, at.Type, at.filename
FROM users_table ut, arbeiten_table at
WHERE at.AuthorID=ut.ID)
WHERE Name = some_name";


This doesn't work for me. If this is not possible, how can I get a similar result?

Thanx and greetings,

Nacho
Reply With Quote
  #2 (permalink)  
Old 11-06-03, 10:13
smithhayward smithhayward is offline
Registered User
 
Join Date: Apr 2003
Location: Edison, NJ / Oakland, NJ (Work)
Posts: 32
When you say it doesn't work do you mean that you get errors or that you don't get the result set that you expected?
__________________
-----------------------------------
Smith Hayward
-----------------------------------
Reply With Quote
  #3 (permalink)  
Old 11-06-03, 10:59
segovia6969 segovia6969 is offline
Registered User
 
Join Date: Nov 2003
Posts: 6
I get an error
Reply With Quote
  #4 (permalink)  
Old 11-06-03, 11:29
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
if it were me doing it, i would simply put the WHERE condition inside the UNION, in each subselect

SELECT Name, Autor, Typ, Dateiname
FROM dokumente
WHERE Name = some_name
UNION
SELECT at.Arbeitsname, ut.Name, at.Type, at.filename
FROM users_table ut
INNER JOIN arbeiten_table at
ON at.AuthorID=ut.ID
WHERE at.Arbeitsname= some_name

if you really want to add the WHERE clause to the outside of the union, you may need to write it like this --

SELECT *
FROM (

SELECT Name, Autor, Typ, Dateiname FROM dokumente)
UNION
SELECT at.Arbeitsname, ut.Name, at.Type, at.filename
FROM users_table ut
INNER JOIN arbeiten_table at
ON at.AuthorID=ut.ID
)
WHERE Name = some_name


rudy
http://r937.com
Reply With Quote
  #5 (permalink)  
Old 11-06-03, 12:16
segovia6969 segovia6969 is offline
Registered User
 
Join Date: Nov 2003
Posts: 6
Great!
Thank you very much for the help. I tried it and it worked.
Greetings,

Nacho
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 On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On