Reputation: 594
This is my query:
SELECT `tositetuotteet`.`id` ,
`tuote` ,
IFNULL( `tositetuotteet`.`hinta` , `tuotteet`.`hinta` ) AS `hinta` ,
`maara` ,
`tosite` ,
`tuotteet`.`nimike`,
`verokannat`.`verokanta`
FROM `tositetuotteet`
JOIN `tuotteet` ON `tuotteet`.`id` = `tositetuotteet`.`tuote`
JOIN `verokannat` ON `tuotteet`.`verokanta` = `verokannat`.`verokanta`
WHERE `tosite` = 1
And you can find my MySQL dump from there.
(SQLFiddle here.)
Query returns nothing. It should return
| id | tuote | hinta | maara | tosite | nimike | verokanta |
| 1 | 1 | 999.99 | 1 | 1 | Esimerkillinen ... | 24 |
What is wrong? I just don't get it.
P.S. If you know better title for this question, go and edit!
Upvotes: 1
Views: 53
Reputation: 18550
Your not connecting the correct column
SELECT `tositetuotteet`.`id` ,
`tuote` ,
IFNULL( `tositetuotteet`.`hinta` , `tuotteet`.`hinta` ) AS `hinta` ,
`maara` ,
`tosite` ,
`tuotteet`.`nimike`,
`verokannat`.`verokanta`
FROM `tositetuotteet`
JOIN `tuotteet` ON `tuotteet`.`id` = `tositetuotteet`.`tuote`
JOIN `verokannat` ON `tuotteet`.`verokanta` = `verokannat`.`id`
WHERE `tosite` = 1
http://sqlfiddle.com/#!2/06192/11
you were connecting verokannat
.verokanta instead of
verokannat.
id
Upvotes: 3
Reputation:
I think you should be linking to verokannat
on its id, not its verokanta value - try:
SELECT `tositetuotteet`.`id` ,
`tuote` ,
IFNULL( `tositetuotteet`.`hinta` , `tuotteet`.`hinta` ) AS `hinta` ,
`maara` ,
`tosite` ,
`tuotteet`.`nimike`,
`verokannat`.`verokanta`
FROM `tositetuotteet`
JOIN `tuotteet` ON `tuotteet`.`id` = `tositetuotteet`.`tuote`
JOIN `verokannat` ON `tuotteet`.`verokanta` = `verokannat`.`id`
WHERE `tosite` = 1
SQLFiddle here.
Upvotes: 1
Reputation: 5215
Replacing JOIN
with LEFT JOIN
did the trick. verokanta
is null so JOIN doesn't return data
SELECT `tositetuotteet`.`id` ,
`tuote` ,
IFNULL( `tositetuotteet`.`hinta` , `tuotteet`.`hinta` ) AS `hinta` ,
`maara` ,
`tosite` ,
`tuotteet`.`nimike`,
`verokannat`.`verokanta`
FROM `tositetuotteet`
LEFT JOIN `tuotteet` ON `tuotteet`.`id` = `tositetuotteet`.`tuote`
LEFT JOIN `verokannat` ON `tuotteet`.`verokanta` = `verokannat`.`verokanta`
WHERE `tosite` = 1;
Upvotes: 0