SQL : add WHERE clause to SELECT with INNER JOIN and UNION

I want to add a WHERE clause to result of the following SQL (I use MySQL):

SELECT 'Marcheur' AS 'type',
    transactions.no_transaction,
    CONCAT(marcheurs.civilite, ' ', marcheurs.prenom, ' ', marcheurs.nom) AS 'nom',
    marcheurs.courriel,
    transactions.montant
FROM transactions
INNER JOIN marcheurs ON marcheurs.no_marcheur = transactions.no_marcheur
UNION
SELECT 'Parrain' AS 'type',
    transactions.no_transaction,
    CONCAT(parrains.civilite, ' ', parrains.prenom, ' ', parrains.nom) AS 'nom',
    parrains.courriel,
    transactions.montant
FROM transactions
INNER JOIN parrains ON parrains.no_parrain = transactions.no_parrain
UNION
SELECT 'Don simple' AS 'type',
    transactions.no_transaction,
    CONCAT(dons_simples.civilite, ' ', dons_simples.prenom, ' ', dons_simples.nom) AS 'nom',
    dons_simples.courriel,
    transactions.montant
FROM transactions
INNER JOIN dons_simples ON dons_simples.no_don = transactions.no_don

I tried but I always get:
Every derived table must have its own alias

The WHERE clause I need is something like
WHERE no_transaction = 1 AND 'type' = "foo" AND nom = "bar" AND courriel = "a@a.com"
mychel_normandeauAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ee_rleeCommented:
try this
SELECT * 
FROM
(SELECT 'Marcheur' AS 'type',
    transactions.no_transaction,
    CONCAT(marcheurs.civilite, ' ', marcheurs.prenom, ' ', marcheurs.nom) AS 'nom',
    marcheurs.courriel,
    transactions.montant
FROM transactions
INNER JOIN marcheurs ON marcheurs.no_marcheur = transactions.no_marcheur
UNION
SELECT 'Parrain' AS 'type',
    transactions.no_transaction,
    CONCAT(parrains.civilite, ' ', parrains.prenom, ' ', parrains.nom) AS 'nom',
    parrains.courriel,
    transactions.montant
FROM transactions
INNER JOIN parrains ON parrains.no_parrain = transactions.no_parrain
UNION
SELECT 'Don simple' AS 'type',
    transactions.no_transaction,
    CONCAT(dons_simples.civilite, ' ', dons_simples.prenom, ' ', dons_simples.nom) AS 'nom',
    dons_simples.courriel,
    transactions.montant
FROM transactions
INNER JOIN dons_simples ON dons_simples.no_don = transactions.no_don) T
WHERE no_transaction = 1 AND 'type' = "foo" AND nom = "bar" AND courriel = "a@a.com"

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mychel_normandeauAuthor Commented:
Damn when I tried I used
AS T
instead of only
T

:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.