MySQL使用CASE连接4个表

时间:2013-10-22 00:03:16

标签: mysql join case

我非常感谢一些帮助开发一个连接4个表但还包含多个CASE语句的查询结构。

我正在尝试生成一个SELECT语句,我可以将其用于客户端网站上的快速查看报告,该报告从成员目录中提取,并根据我输入的值显示自定义配置文件字段的值。

这是我的数据库结构(已清理的提取)。

--
-- Table structure for TABLE cats
--

CREATE TABLE cats (
cat_id INT(12) NOT NULL auto_increment,
cat_name VARCHAR(64),
cat_parent INT(12),
primary KEY (cat_id));

--
-- Dumping data for TABLE cats
--
INSERT INTO cats VALUES(1, 'Root',  0);
INSERT INTO cats VALUES(2, 'GroupA', 1);
INSERT INTO cats VALUES(3, 'GroupB', 1);
INSERT INTO cats VALUES(4, 'ClassA', 2);
INSERT INTO cats VALUES(5, 'ClassB', 2);
INSERT INTO cats VALUES(6, 'ClassC', 2);
INSERT INTO cats VALUES(7, 'ClassD', 3);
INSERT INTO cats VALUES(8, 'ClassE', 3);
INSERT INTO cats VALUES(9, 'ClassF', 3);

-- --------------------------------------------------------

--
-- Table structure for TABLE catlink
--

CREATE TABLE catlink (
 cl_id int(11) NOT NULL auto_increment,
 link_id int(11) NOT NULL default '0',
 cat_id int(11) NOT NULL default '0',
primary KEY (cl_id));


--
-- Dumping data for TABLE catlink
--

INSERT INTO catlink VALUES(1, 1, 3);
INSERT INTO catlink VALUES(2, 2, 4);
INSERT INTO catlink VALUES(3, 3, 5);
INSERT INTO catlink VALUES(4, 4, 6);
INSERT INTO catlink VALUES(5, 5, 7);
INSERT INTO catlink VALUES(6, 6, 8);

-- --------------------------------------------------------

--
-- Table structure for TABLE links
--

CREATE TABLE links (
 link_id int(11) NOT NULL auto_increment,
 link_name varchar(255) NOT NULL,
primary KEY (link_id));

--
-- Dumping data for TABLE links
--


INSERT INTO links VALUES(1, 'Link One');
INSERT INTO links VALUES(2, 'Link Two');
INSERT INTO links VALUES(3, 'Link Three');
INSERT INTO links VALUES(4, 'Link Four');
INSERT INTO links VALUES(5, 'Link Five');
INSERT INTO links VALUES(6, 'Link Six');

-- -------------------------------------------------------

--
-- Table structure for TABLE cfvalues
--

CREATE TABLE cfvalues (
 id int(11) NOT NULL auto_increment,
 cf_id int(11) NOT NULL,
 link_id int(11) NOT NULL,
 value mediumtext NOT NULL,
primary KEY (id));
--
-- Dumping data for TABLE cfvalues
--

INSERT INTO cfvalues VALUES(1, 54, 1, 'Link One name');
INSERT INTO cfvalues VALUES(2, 54, 2, 'Link Two name');
INSERT INTO cfvalues VALUES(3, 54, 3, 'Link Three name');
INSERT INTO cfvalues VALUES(4, 54, 4, 'Link Four name');
INSERT INTO cfvalues VALUES(5, 54, 5, 'Link Five name');
INSERT INTO cfvalues VALUES(6, 54, 6, 'Link Six name');
INSERT INTO cfvalues VALUES(7, 54, 7, 'Link Seven name');
INSERT INTO cfvalues VALUES(8, 54, 8, 'Link Eight name');
INSERT INTO cfvalues VALUES(9, 55, 1, 'Link One custom value');
INSERT INTO cfvalues VALUES(10, 55, 2, 'Link Two custom value');
INSERT INTO cfvalues VALUES(11, 55, 3, 'Link Three custom value');
INSERT INTO cfvalues VALUES(12, 55, 4, 'Link Four custom value');
INSERT INTO cfvalues VALUES(13, 55, 5, 'Link Five custom value');
INSERT INTO cfvalues VALUES(14, 55, 6, 'Link Six custom value');
INSERT INTO cfvalues VALUES(15, 55, 7, 'Link Seven custom value');
INSERT INTO cfvalues VALUES(16, 55, 8, 'Link Eight custom value');

这是我的(完全没有工作)查询。

Select cats.cat_name, links.link_name, cfvalues.value 
MAX(CASE WHEN cfvalues.cf_id =54 THEN cfvalues.value END ) Name,
MAX(CASE WHEN cfvalues.cf_id =55 THEN cfvalues.value END ) Custom,
FROM cfvalues
INNER JOIN links ON links.link_id = cfvalues.link_id 
INNER JOIN catlink ON links.link_id = catlink.link_id
INNER JOIN cats ON cats.cat_id = catlink.cat_id
GROUP BY cfvalues.link_id;

这就是我得到的:

| CAT_NAME |  LINK_NAME |           VALUE |
|----------|------------|-----------------|
|   ClassA |   Link One |   Link One name |
|   ClassB |   Link Two |   Link Two name |
|   ClassC | Link Three | Link Three name |
|   ClassD |  Link Four |  Link Four name |
|   ClassE |  Link Five |  Link Five name |
|   ClassF |   Link Six |   Link Six name |

这就是我想要的:

| CAT_NAME |  LINK_NAME |            NAME |                  CUSTOM |
|----------|------------|-----------------|-------------------------|
|   ClassA |   Link One |   Link One name |   Link One custom value |
|   ClassB |   Link Two |   Link Two name |   Link Two custom value |
|   ClassC | Link Three | Link Three name | Link Three custom value |
|   ClassD |  Link Four |  Link Four name |  Link Four custom value |
|   ClassE |  Link Five |  Link Five name |  Link Five custom value |
|   ClassF |   Link Six |   Link Six name |   Link Six custom value |

请注意,列名也需要在查询中更新,以便结果可以导出为具有唯一列标题的CSV,而不是值,值,值等。

有什么建议吗?我不是自称是开发人员,所以我感谢任何帮助! 谢谢:))

1 个答案:

答案 0 :(得分:0)

使用子查询的可能解决方案:

select 
    c.cat_name,
    l.link_name,
    (select value from cfvalues cfv where cfv.link_id=l.link_id and cfv.cf_id=54) as name,
    (select value from cfvalues cfv where cfv.link_id=l.link_id and cfv.cf_id=55) as custom
from 
    cats c
    INNER JOIN catlink cl ON c.cat_id = cl.cat_id
    INNER JOIN links l ON l.link_id = cl.link_id 

替代使用if语句:

select 
    c.cat_name,
    l.link_name,
    max(if(cfv.cf_id=54, cfv.value, '')) as name,
    max(if(cfv.cf_id=55, cfv.value, ''))  as custom
from 
    cats c
    INNER JOIN catlink cl ON c.cat_id = cl.cat_id
    INNER JOIN links l ON l.link_id = cl.link_id 
    INNER JOIN cfvalues cfv ON cfv.link_id = l.link_id
group by c.cat_id, l.link_id

替代使用案例:

select 
    c.cat_name,
    l.link_name,
    max(case cfv.cf_id when 54 then cfv.value else '' end) as name,
    max(case cfv.cf_id when 55 then cfv.value else '' end)  as custom
from 
    cats c
    INNER JOIN catlink cl ON c.cat_id = cl.cat_id
    INNER JOIN links l ON l.link_id = cl.link_id 
    INNER JOIN cfvalues cfv ON cfv.link_id = l.link_id
group by c.cat_id, l.link_id