我现在进行了很多尝试,但没有任何帮助。 我想创建一个灵活的 View (如果可能)。结果是 2 个表格。 这是我想要的:
表访问者:
ID Hash created
1 sadasdasdasd 2017-02-24 08:31:37
2 123kipdösas3 2017-02-24 08:31:37
3 12oaskdasdap 2017-02-24 08:31:37
4 asdauihsuahs 2017-02-24 08:31:37
表访客数据
id vID type value created
1 1 First Name Hans 2017-02-24 09:15:11
2 1 Last Name Wurst 2017-02-24 09:15:11
3 1 Company WDHUWDHUGMBGH 2017-02-24 09:15:11
4 1 E-Mail asddas@asdsd.de 2017-02-24 09:15:11
5 1 Phone 123123 2017-02-24 09:15:11
6 1 Salutation Mr 2017-02-24 09:15:11
7 1 Type qwes 2017-02-24 09:15:11
最后我希望我的表( View )像这样:
id vID First Name Last Name Company E-Mail Phone Salutation Type
1 1 Hans Wurst WDHUWDHUGMBGH asddas@asdsd.de 123123 Mr qwes
事实是我们不知道 VisitorData.type 中可以包含什么。 所以我不能这样做
CREATE VIEW DataVisitorView AS
SELECT DataVisitor.id, DataVisitor.hash,
IF(DataVisitorValues.Type='First Name',DataVisitorValues.Value,null) AS First Name,
IF(DataVisitorValues.Type='Last Name',DataVisitorValues.Value,null) AS Last Name ,
IF(DataVisitorValues.Type=' Company ',DataVisitorValues.Value,null) AS Company ,
IF(DataVisitorValues.Type='E-Mail',DataVisitorValues.Value,null) AS E-Mail,
IF(DataVisitorValues.Type='Phone ',DataVisitorValues.Value,null) AS Phone ,
IF(DataVisitorValues.Type='Salutation ',DataVisitorValues.Value,null) AS Salutation
FROM `DataVisitor`
LEFT JOIN `DataVisitorValues`
ON `DataVisitor`.`id` = `DataVisitorValues`.`vID`
所以我在考虑更多
CREATE VIEW `DataVisitorView` AS
SELECT
DataVisitorValues2.typ
FROM (`DataVisitor2`
LEFT JOIN `DataVisitorValues2`
ON `DataVisitor2`.`id` = `DataVisitorValues2`.`vID`)
Where `DataVisitor2`.`id` = `DataVisitorValues2`.`vID`
但这完全错了,我得到了一张 table
|typ|
First Name
Last Name
Company
...
有解决办法吗? 我也使用 Php 来完成这项工作,但是表格会自动更新自己,而 chrone 工作在这里没有任何帮助
最佳答案
您不能创建动态 View 或其中包含变量的 View 。 所以需要动态查询。
创建表/插入数据
CREATE TABLE Visitor
(`ID` INT, `Hash` VARCHAR(12), `created` VARCHAR(19))
;
INSERT INTO Visitor
(`ID`, `Hash`, `created`)
VALUES
(1, 'sadasdasdasd', '2017-02-24 08:31:37'),
(2, '123kipdösas3', '2017-02-24 08:31:37'),
(3, '12oaskdasdap', '2017-02-24 08:31:37'),
(4, 'asdauihsuahs', '2017-02-24 08:31:37')
;
CREATE TABLE VisitorData
(`id` INT, `vID` INT, `type` VARCHAR(10), `value` VARCHAR(15), `created` VARCHAR(19))
;
INSERT INTO VisitorData
(`id`, `vID`, `type`, `value`, `created`)
VALUES
(1, 1, 'First Name', 'Hans', '2017-02-24 09:15:11'),
(2, 1, 'Last Name', 'Wurst', '2017-02-24 09:15:11'),
(3, 1, 'Company', 'WDHUWDHUGMBGH', '2017-02-24 09:15:11'),
(4, 1, 'E-Mail', 'asddas@asdsd.de', '2017-02-24 09:15:11'),
(5, 1, 'Phone', '123123', '2017-02-24 09:15:11'),
(6, 1, 'Salutation', 'Mr', '2017-02-24 09:15:11'),
(7, 1, 'Type', 'qwes', '2017-02-24 09:15:11')
;
查询
您想要的是称为数据透视表的输出。 这是通过 GROUP BY 和像这样使用函数 MAX 完成的。
SELECT
Visitor.id
, VisitorData.vid
, MAX(CASE WHEN VisitorData.type = 'First Name' THEN VisitorData.value ELSE NULL END) AS 'First_name'
, MAX(CASE WHEN VisitorData.type = 'Last Name' THEN VisitorData.value ELSE NULL END) AS 'Last_Name'
, MAX(CASE WHEN VisitorData.type = 'Company' THEN VisitorData.value ELSE NULL END) AS 'Company'
, MAX(CASE WHEN VisitorData.type = 'E-Mail' THEN VisitorData.value ELSE NULL END) AS 'E-Mail'
, MAX(CASE WHEN VisitorData.type = 'Phone' THEN VisitorData.value ELSE NULL END) AS 'Phone'
, MAX(CASE WHEN VisitorData.type = 'Salutation' THEN VisitorData.value ELSE NULL END) AS 'Salutation'
, MAX(CASE WHEN VisitorData.type = 'Type' THEN VisitorData.value ELSE NULL END) AS 'Type'
FROM
Visitor
INNER JOIN
VisitorData
ON
Visitor.id = VisitorData.vid
GROUP BY
Visitor.id
, VisitorData.vid
ORDER BY
Visitor.id
, VisitorData.vid
结果
id vid First_name Last_Name Company E-Mail Phone Salutation Type
------ ------ ---------- --------- ------------- --------------- ------ ---------- --------
1 1 Hans Wurst WDHUWDHUGMBGH asddas@asdsd.de 123123 Mr qwes
要使列动态化,您可以使用它。
SET SESSION group_concat_max_len = 1000000 -- enlarge GROUP_CONCAT output to 10 Mb
SET @group_concat_sql = NULL;
# generate all MAX lines
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(CASE WHEN VisitorData.type = \'',VisitorData.type,'\' THEN VisitorData.value ELSE NULL END) AS \'',VisitorData.type,'\''
)
)
INTO @group_concat_sql
FROM
VisitorData;
# generate complete SQL query
SET @sql =
CONCAT(
'SELECT
Visitor.id
, VisitorData.vid
'
, ','
, @group_concat_sql
, 'FROM
Visitor
INNER JOIN
VisitorData
ON
Visitor.id = VisitorData.vid
GROUP BY
Visitor.id
, VisitorData.vid
ORDER BY
Visitor.id
, VisitorData.vid
'
);
# execute SQL query
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
结果
id vid First Name Last Name Company E-Mail Phone Salutation Type
------ ------ ---------- --------- ------------- --------------- ------ ---------- --------
1 1 Hans Wurst WDHUWDHUGMBGH asddas@asdsd.de 123123 Mr qwes
关于Mysql 从 SQL 查询创建灵活的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42434126/