dotaz - vícenásobný JOINmám tři cvičné tabulky:
-- Adminer 3.3.4 MySQL dump
SET NAMES utf8;
SET foreign_key_checks = 0;
SET time_zone = 'SYSTEM';
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
USE `puschpullorg_portfolio`;
DROP TABLE IF EXISTS `abc_table_a`;
CREATE TABLE `abc_table_a` (
`id_column_a_1` int(10) NOT NULL auto_increment,
`column_a_2` varchar(64) NOT NULL,
`column_a_3` int(10) NOT NULL,
PRIMARY KEY (`id_column_a_1`),
KEY `column_a_3` (`column_a_3`),
CONSTRAINT `abc_table_a_ibfk_1` FOREIGN KEY (`column_a_3`) REFERENCES `abc_table_b` (`id_column_b_1`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `abc_table_a` (`id_column_a_1`, `column_a_2`, `column_a_3`) VALUES
(1, 'Adam', 4),
(2, 'Bruno', 1),
(3, 'Carl', 2),
(4, 'Mary', 2),
(5, 'John', 3),
(6, 'Paul', 3),
(7, 'Nancy', 1);
DROP TABLE IF EXISTS `abc_table_b`;
CREATE TABLE `abc_table_b` (
`id_column_b_1` int(10) NOT NULL auto_increment,
`column_b_2` varchar(64) NOT NULL,
`column_b_3` int(10) NOT NULL,
PRIMARY KEY (`id_column_b_1`),
KEY `column_b_3` (`column_b_3`),
CONSTRAINT `abc_table_b_ibfk_1` FOREIGN KEY (`column_b_3`) REFERENCES `abc_table_c` (`id_column_c_1`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `abc_table_b` (`id_column_b_1`, `column_b_2`, `column_b_3`) VALUES
(1, 'Child', 1),
(2, 'Student', 1),
(3, 'Worker', 1),
(4, 'Soldier', 1);
DROP TABLE IF EXISTS `abc_table_c`;
CREATE TABLE `abc_table_c` (
`id_column_c_1` int(10) NOT NULL auto_increment,
`column_c_2` varchar(64) NOT NULL,
PRIMARY KEY (`id_column_c_1`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `abc_table_c` (`id_column_c_1`, `column_c_2`) VALUES
(1, 'London'),
(2, 'Dublin'),
(3, 'Paris'),
(4, 'Madrid'),
(5, 'Prague');
-- 2012-03-29 07:40:23
a chci zkusit propojení
do výsledné tabulky dotazem:
SELECT id_column_a_1, column_a_2, column_a_3, column_b_2, id_column_b_1, column_c_2, id_column_c_1
FROM
((`abc_table_a` LEFT JOIN `abc_table_b` ON abc_table_a.column_a_3 = abc_table_b.id_column_b_1)
LEFT JOIN `abc_table_c` ON abc_table_b.column_b_2 = abc_table_c.id_column_c_1)
ORDER BY column_b_2 DESC
LIMIT 10;
vysledek je bohužel toto:
Jak dosáhnout aby poslední dva sloupce výsledné tabulky obsahovala data ze třetí tabulky?
Děkuji
|