我有一个名为章节的表格,其中有5个字段用于书籍(book1,book2等)和5个字段用于产品(product1,product2等),还有一些其他字段对于此不应该是必需的,但我已经包含了每个记录唯一的密钥(文件名),以防万一。
章节
| filename | title | book1 | book2 | book3 | book4 | book5 | product1 | product2 | product3 | product4 | product5 |
|------------|---------|---------|-------|--------|-------|-------|-----------|----------|-----------|----------|----------|
| file1.pdf | Title1 | Book1 | | | | | Product4 | | | | |
| file2.pdf | Title2 | Book9 | | | | | Product1 | Product4 | | | |
| file3.pdf | Title3 | Book4 | | | | | Product5 | | | | |
| file4.pdf | Title4 | Book7 | | | | | Product1 | | | | |
| file5.pdf | Title5 | Book21 | | | | | Product13 | Product9 | Product11 | | |
| file6.pdf | Title6 | Book5 | | | | | Product23 | Product3 | | | |
| file7.pdf | Title7 | Book675 | | | | | Product45 | Product6 | | | |
| file8.pdf | Title8 | Book3 | Book5 | | | | Product9 | | | | |
| file9.pdf | Title9 | Book1 | Book7 | Book34 | | | Product77 | | | | |
| file10.pdf | Title10 | Book22 | | | | | Product3 | | | | |
我最终需要做2个查询。首先是获得所有可能书籍的列表,以及任何相关产品,第二个是镜像,所有可能的产品,以及相关书籍列表。基本相同,只是信息的一面镜子。这是一个理想的最终结果。
| books | products |
|---------|--------------------------------|
| Book1 | Product4, Product77 |
| Book21 | Product13, Product9, Product11 |
| Book22 | Product3 |
| Book3 | Product9 |
| Book34 | Product77 |
| Book4 | Product5 |
| Book5 | Product9, Product23, Product3 |
| Book675 | Product45, Product6 |
| Book7 | Product77, Product1 |
| Book9 | Product1, Product4 |'
我已经尝试过大量的选择(不要问多少时间),但以下内容让我非常接近,但我得到了空白的书籍清单,我得到了重复(book1会出现两次,等等。 )。
SELECT booklist.books,
concat_ws(', ',
IF(LENGTH(chapters.product1),chapters.product1, NULL),
IF(LENGTH(chapters.product2),chapters.product2, NULL),
IF(LENGTH(chapters.product3),chapters.product3, NULL),
IF(LENGTH(chapters.product4),chapters.product4, NULL),
IF(LENGTH(chapters.product5),chapters.product5, NULL))
AS products
FROM
(SELECT book1 AS books FROM chapters
UNION
SELECT book2 FROM chapters
UNION
SELECT book3 FROM chapters
UNION
SELECT book4 FROM chapters
UNION
SELECT book5 FROM chapters) booklist
JOIN
chapters ON chapters.book1 = booklist.books
OR chapters.book2 = booklist.books
OR chapters.book3 = booklist.books
OR chapters.book4 = booklist.books
OR chapters.book5 = booklist.books
ORDER BY booklist.books;
给了我以下内容:
| books | products |
|---------|--------------------------------|
| | Product13, Product9, Product11 |
| | Product1 |
| | Product5 |
| | Product1, Product4 |
| | Product3 |
| | Product4 |
| | Product77 |
| | Product9 |
| | Product45, Product6 |
| | Product23, Product3 |
| Book1 | Product4 |
| Book1 | Product77 |
| Book21 | Product13, Product9, Product11 |
| Book22 | Product3 |
| Book3 | Product9 |
| Book34 | Product77 |
| Book4 | Product5 |
| Book5 | Product9 |
| Book5 | Product23, Product3 |
| Book675 | Product45, Product6 |
| Book7 | Product77 |
| Book7 | Product1 |
| Book9 | Product1, Product4 |
所以最后,我如何摆脱空记录并合并多本书。数据集永远不会超过200-300条记录。感谢您阅读这篇文章!
答案 0 :(得分:2)
在不了解更多关于文件,标题,书籍和产品以及它们如何相互关联的情况下,很难明确地回答 - 但是当您开始规范化时,请考虑以下内容......
DROP TABLE IF EXISTS file_title;
CREATE TABLE file_title
(file_title_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,file VARCHAR(20) NOT NULL
,title VARCHAR(20) NOT NULL
,UNIQUE(file,title)
);
INSERT INTO file_title VALUES
( 1,'file1.pdf' , 'Title1' ),
( 2,'file2.pdf' , 'Title2' ),
( 3,'file3.pdf' , 'Title3' ),
( 4,'file4.pdf' , 'Title4' ),
( 5,'file5.pdf' , 'Title5' ),
( 6,'file6.pdf' , 'Title6' ),
( 7,'file7.pdf' , 'Title7' ),
( 8,'file8.pdf' , 'Title8' ),
( 9,'file9.pdf' , 'Title9' ),
(10,'file10.pdf' , 'Title10');
DROP TABLE IF EXISTS books;
CREATE TABLE books
(file_title_id INT NOT NULL
,book_id INT NOT NULL
,book VARCHAR(20) NOT NULL
,PRIMARY KEY(file_title_id,book_id)
);
INSERT INTO books VALUES
( 1,1,'Book1'),
( 2,1,'Book9'),
( 3,1,'Book4'),
( 4,1,'Book7'),
( 5,1,'Book21'),
( 6,1,'Book5'),
( 7,1,'Book675'),
( 8,1,'Book3'),
( 9,1,'Book1'),
(10,1,'Book22'),
( 8,2,'Book5'),
( 9,2,'Book7'),
( 9,3,'Book34');
DROP TABLE IF EXISTS products;
CREATE TABLE products
(file_title_id INT NOT NULL
,product_id INT NOT NULL
,product VARCHAR(20) NOT NULL
,PRIMARY KEY(file_title_id,product_id)
);
INSERT INTO products VALUES
( 1,1 , 'Product4'),
( 2,1 , 'Product1'),
( 3,1 , 'Product5'),
( 4,1 , 'Product1'),
( 5,1 , 'Product13'),
( 6,1 , 'Product23'),
( 7,1 , 'Product45'),
( 8,1 , 'Product9'),
( 9,1 , 'Product77'),
(10,1 , 'Product3'),
( 2,2 , 'Product4'),
( 5,2 , 'Product9'),
( 6,2 , 'Product3'),
( 7,2 , 'Product6'),
( 5,3 , 'Product11');
SELECT * FROM file_title;
+---------------+------------+---------+
| file_title_id | file | title |
+---------------+------------+---------+
| 1 | file1.pdf | Title1 |
| 10 | file10.pdf | Title10 |
| 2 | file2.pdf | Title2 |
| 3 | file3.pdf | Title3 |
| 4 | file4.pdf | Title4 |
| 5 | file5.pdf | Title5 |
| 6 | file6.pdf | Title6 |
| 7 | file7.pdf | Title7 |
| 8 | file8.pdf | Title8 |
| 9 | file9.pdf | Title9 |
+---------------+------------+---------+
SELECT * FROM books;
+---------------+---------+---------+
| file_title_id | book_id | book |
+---------------+---------+---------+
| 1 | 1 | Book1 |
| 2 | 1 | Book9 |
| 3 | 1 | Book4 |
| 4 | 1 | Book7 |
| 5 | 1 | Book21 |
| 6 | 1 | Book5 |
| 7 | 1 | Book675 |
| 8 | 1 | Book3 |
| 8 | 2 | Book5 |
| 9 | 1 | Book1 |
| 9 | 2 | Book7 |
| 9 | 3 | Book34 |
| 10 | 1 | Book22 |
+---------------+---------+---------+
SELECT * FROM products;
+---------------+------------+-----------+
| file_title_id | product_id | product |
+---------------+------------+-----------+
| 1 | 1 | Product4 |
| 2 | 1 | Product1 |
| 2 | 2 | Product4 |
| 3 | 1 | Product5 |
| 4 | 1 | Product1 |
| 5 | 1 | Product13 |
| 5 | 2 | Product9 |
| 5 | 3 | Product11 |
| 6 | 1 | Product23 |
| 6 | 2 | Product3 |
| 7 | 1 | Product45 |
| 7 | 2 | Product6 |
| 8 | 1 | Product9 |
| 9 | 1 | Product77 |
| 10 | 1 | Product3 |
+---------------+------------+-----------+
SELECT ft.*
, GROUP_CONCAT(DISTINCT book ORDER BY book_id) books
, GROUP_CONCAT(DISTINCT product ORDER BY product_id) products
FROM file_title ft
LEFT
JOIN books b
ON b.file_title_id = ft.file_title_id
LEFT
JOIN products p
ON p.file_title_id = ft.file_title_id
GROUP
BY file_title_id;
+---------------+------------+---------+--------------------+------------------------------+
| file_title_id | file | title | books | products |
+---------------+------------+---------+--------------------+------------------------------+
| 1 | file1.pdf | Title1 | Book1 | Product4 |
| 2 | file2.pdf | Title2 | Book9 | Product1,Product4 |
| 3 | file3.pdf | Title3 | Book4 | Product5 |
| 4 | file4.pdf | Title4 | Book7 | Product1 |
| 5 | file5.pdf | Title5 | Book21 | Product13,Product9,Product11 |
| 6 | file6.pdf | Title6 | Book5 | Product23,Product3 |
| 7 | file7.pdf | Title7 | Book675 | Product45,Product6 |
| 8 | file8.pdf | Title8 | Book3,Book5 | Product9 |
| 9 | file9.pdf | Title9 | Book1,Book7,Book34 | Product77 |
| 10 | file10.pdf | Title10 | Book22 | Product3 |
+---------------+------------+---------+--------------------+------------------------------+