[英]MySQL select unique row entries of a column as columns in a new view or table
I have a dilemma. 我有两难选择。 I am using a CMS that creates a database table where the records are populated as follows: 我正在使用CMS创建数据库表,其中记录的填充如下:
+------------+----------------+
| field_name | field_value |
+------------+----------------+
| your-name | Adam |
| your-email | adam@email.com |
| your-name | Ben |
| your-email | en@email.com |
+------------+----------------+
where field_name and field_value are column names. 其中field_name和field_value是列名。 Also, 'your-name' and 'your-email' are are the input names of forms in a webpage. 另外,“您的姓名”和“您的电子邮件”是网页中表单的输入名称。
(there are other columns but none of them have a unique row entry) (还有其他列,但它们都没有唯一的行条目)
My question is, how do i create a table or view so that I can get to this from the above table? 我的问题是,如何创建表或视图,以便可以从上表中访问该表或视图?
+-----------+----------------+
| your-name | your-email |
+-----------+----------------+
| Adam | Adam@email.com |
| Ben | Ben@email.com |
+-----------+----------------+
I have thought about creating views and joining them but I cannot seem to get it working. 我曾考虑过创建视图并加入它们,但似乎无法使其正常工作。
Any ideas how to implement this? 任何想法如何实现这一点?
Thank you! 谢谢!
Doesn't your table 1 have kind of an id column like this (so that (id, field_name) is the primary key of this table)? 您的表1是否没有这样的id列(因此,(id,field_name)是该表的主键)?
+----+------------+----------------+
| id | field_name | field_value |
+----+------------+----------------+
| 1 | your-name | Adam |
| 1 | your-email | adam@email.com |
| 2 | your-name | Ben |
| 2 | your-email | en@email.com |
+----+------------+----------------+
Then you could do... 那你可以做...
SELECT DISTINCT myTableAlias.id,
(SELECT field_value FROM table
WHERE id=myTableAlias.id AND field_name = 'your-name') your-name,
(SELECT field_value FROM table
WHERE id=myTableAlias.id AND field_name = 'your-email') your-email,
FROM table myTableAlias
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.