如何在一个查询中从三个表中进行选择

时间:2015-05-27 17:00:56

标签: php mysql sql

我有以下数据库结构:

db_config_list id | id_config | type

DB_CONFIG id | id_list | name | value

db_config_lang id | id_list | id_lang | name | value

在一个mySQL查询中,我想从db_configdb_config_lang中选择db_config.id_list = db_config_list.iddb_config_lang.id_list = db_config_list.id

我试过了:

$sql = 'SELECT * FROM `'._DB_PREFIX_.$this->db_config_list.'` cl
        LEFT JOIN `'._DB_PREFIX_.$this->db_config.'` c
        ON (cl.id = c.id_list)
        LEFT JOIN `'._DB_PREFIX_.$this->db_config_lang.'` cll
        ON (cl.id = cll.id_list)
        WHERE cl.id_config = '.$this->c_id.'
        AND cl.type = "'.$this->c_type.'"
        AND cll.id_lang = "'.$this->default_lang.'"';

但它无法正常工作。当我在两个不同的查询中执行此操作时,它实际上有效:

$sql = 'SELECT * FROM `'._DB_PREFIX_.$this->db_config_list.'` cl
        LEFT JOIN `'._DB_PREFIX_.$this->db_config.'` c
        ON (cl.id = c.id_list)
        WHERE cl.id_config = '.$this->c_id.'
        AND cl.type = "'.$this->c_type.'"';

$sql = 'SELECT * FROM `'._DB_PREFIX_.$this->db_config_list.'` cl
        LEFT JOIN `'._DB_PREFIX_.$this->db_config_lang.'` cll
        ON (cl.id = cll.id_list)
        WHERE cll.id_lang = "'.$this->default_lang.'"';

但我想在一个查询中执行此操作。这可能吗?

//编辑

我认为我试图实现一些不可能的事情。所以我选择了不同的解决方案

$sql = 'SELECT name,value FROM `'._DB_PREFIX_.$this->db_config_list.'` cl
        LEFT JOIN `'._DB_PREFIX_.$this->db_config.'` c
        ON (cl.id = c.id_list)
        WHERE cl.id_config = '.$this->c_id.'
        AND cl.type = "'.$this->c_type.'"';

$sql2 = 'SELECT name,value FROM `'._DB_PREFIX_.$this->db_config_list.'` cl
        LEFT JOIN `'._DB_PREFIX_.$this->db_config_lang.'` cll
        ON (cl.id = cll.id_list)
        WHERE cll.id_lang = "'.$this->default_lang.'"';

$query1 =  Db::getInstance()->executeS($sql);
$query2 =  Db::getInstance()->executeS($sql2);

$query = array_merge($query1, $query2);

//编辑2

以下是sqlfiddle.com的代码

创建代码:

CREATE TABLE IF NOT EXISTS `db_config_list` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `id_config` int(10) unsigned NOT NULL,
    `type` varchar(10) NOT NULL,
    PRIMARY KEY (`id`)
) DEFAULT CHARSET=UTF8;

CREATE TABLE IF NOT EXISTS `db_config` (
    `id_list` int(10) unsigned NOT NULL,
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `name` varchar(255) NOT NULL,
    `value` text NOT NULL,
    PRIMARY KEY (`id`)
) DEFAULT CHARSET=UTF8;


CREATE TABLE IF NOT EXISTS `db_config_lang` (
    `id_list` int(10) unsigned NOT NULL,
    `id_lang` int(10) unsigned NOT NULL,
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `name` varchar(255) NOT NULL,
    `value` text NOT NULL,
    PRIMARY KEY (`id`)
) DEFAULT CHARSET=UTF8;

ALTER TABLE `db_config` ADD INDEX(`id_list`);

ALTER TABLE `db_config_lang` ADD INDEX(`id_list`);

ALTER TABLE `db_config` ADD FOREIGN KEY (`id_list`) REFERENCES `db_config_list`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE `db_config_lang` ADD FOREIGN KEY (`id_list`) REFERENCES `db_config_list`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

INSERT INTO `db_config_list` (`id`, `id_config`, `type`) VALUES
(1, 1, 'global');

INSERT INTO `db_config` (`id_list`, `id`, `name`, `value`) VALUES
(1, 1, 'font_family', ''),
(1, 2, 'first_main_color', '#19BCE7'),
(1, 3, 'use_background_image', '1'),
(1, 4, 'background_color', '#F60'),
(1, 5, 'animated_tabs_carousel', '1'),
(1, 6, 'boxed_layout', '1'),
(1, 7, 'loading_animation', '1'),
(1, 8, 'smooth_scroll', '1'),
(1, 9, 'responsiveness', '1'),
(1, 10, 'sticky_header', '1');

INSERT INTO `db_config_lang` (`id_list`, `id_lang`, `id`, `name`, `value`) VALUES
(1, 1, 1, 'center_column_content', 'boo foo'),
(1, 1, 2, 'bottom_column_content', 'boo foo'),
(1, 2, 3, 'center_column_content', 'boo foo'),
(1, 2, 4, 'bottom_column_content', 'boo foo'),
(1, 3, 5, 'center_column_content', 'boo foo'),
(1, 3, 6, 'bottom_column_content', 'boo foo'),
(1, 4, 7, 'center_column_content', 'boo foo'),
(1, 4, 8, 'bottom_column_content', 'boo foo');

选择代码:

SELECT name,value FROM `db_config_list` cl
LEFT JOIN `db_config` c
ON (cl.id = c.id_list)
WHERE cl.id_config = 1
AND cl.type = "global";

SELECT name,value FROM `db_config_list` cl
LEFT JOIN `db_config_lang` cll
ON (cl.id = cll.id_list)
WHERE cll.id_lang = 1
AND cl.id_config = 1
AND cl.type = "global";

1 个答案:

答案 0 :(得分:3)

我认为正在发生的事情更可能只是select是一个*属性 - 它只会为每个列名选择一个值,而不管有多少表被连接。如果您可以为此设置发布SQL Fiddle,我可以帮助您修改查询。从本质上讲,我相信你会做类似的事情:

$sql = 'SELECT *, c.name AS config_name, cll.name as config_lang_name
        FROM `'._DB_PREFIX_.$this->db_config_list.'` cl
        LEFT JOIN `'._DB_PREFIX_.$this->db_config.'` c
        ON (cl.id = c.id_list)
        LEFT JOIN `'._DB_PREFIX_.$this->db_config_lang.'` cll
        ON (cl.id = cll.id_list)
        WHERE cl.id_config = '.$this->c_id.'
        AND cl.type = "'.$this->c_type.'"
        AND cll.id_lang = "'.$this->default_lang.'"';

修改 执行两个单独的查询然后组合结果集将比调整查询以返回所需的结果集慢得多。这个查询更接近你想要的吗? (上面只是一个例子,既然你已经展示了你的最终目标,我相信这应该有效。)

$sql = 'SELECT cl.name AS config_list_name, cl.value AS config_list_value,
        cll.name AS config_lang_name, cll.value AS config_lang_value
        FROM `'._DB_PREFIX_.$this->db_config_list.'` cl
        LEFT JOIN `'._DB_PREFIX_.$this->db_config.'` c
        ON (cl.id = c.id_list)
        LEFT JOIN `'._DB_PREFIX_.$this->db_config_lang.'` cll
        ON (cl.id = cll.id_list)
        WHERE cl.id_config = '.$this->c_id.'
        AND cl.type = "'.$this->c_type.'"
        AND cll.id_lang = "'.$this->default_lang.'"';

编辑2: 看起来您正在使用此查询的框架。我真的建议使用一些参数绑定,而不是将值直接放入查询中。