我将非常感谢开发连接 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。
有什么建议么?我不声称自己是开发人员,所以我很感激我能得到的任何帮助!谢谢 :)