简体   繁体   English

从现有值在mysql视图中创建新列

[英]Creating a new column in mysql view from an existing value

I am incredibly new at this so let me know if I can clarify. 我在这方面是个新手,所以请让我知道是否可以澄清。 Essentially I am looking to create a new column in a mysql view based on values from a column in an existing table. 本质上,我希望基于现有表中某个列的值在mysql视图中创建一个新列。 I'm using phpGrid to output on the front end. 我正在使用phpGrid在前端输出。

For instance - let's say I have existing column question_id : 例如-假设我已有question_id

    name | question_id | answer
----------------------------------
    Joe  | 1           | yes
    Joe  | 2           | no
    Ann  | 1           | maybe
    Ann  | 2           | kinda

and I have custom view my_view where I want the columns to be as such (not necessarily with question_id= in front, but whatever works): 并且我有自定义视图my_view ,我希望列是这样的(不一定在前面带有question_id= ,但是可以用):

    name  | question_id=1 | question_id=2
------------------------------------------
    Joe   | yes           | no
    Ann   | maybe         | kinda

Or something to that effect. 或类似的东西。 How do I get the question_id values to become columns, and the answers in the right place? 如何获得question_id值成为列,并在正确的位置给出答案?

Also, if name is from one table, and question_id is from another, how can I get the correctly associated values alongside each other in my_view ? 另外,如果name来自一个表,而question_id来自另一个表,如何在my_view正确关联的值彼此my_view name values often have duplicates, but they have individual entry ids. name值通常具有重复项,但是它们具有单独的条目ID。

Cheers and thanks! 干杯,谢谢!

EDIT: Create MySQL view using distinct values as columns is the right idea, but I get error #1064 about incorrect syntax. 编辑: 使用不同的值作为列创建MySQL视图是正确的主意,但我收到有关语法错误的错误#1064。

Also, I'm not sure it solves the second problem of getting the data to align by unique entry ID. 另外,我不确定它是否解决了第二个问题,即通过唯一的条目ID来使数据对齐。 I tried to use this: 1052: Column 'id' in field list is ambiguous to fix the ambiguous issue but then I get error #1060 that there is a duplicate. 我尝试使用此功能: 1052:字段列表中的列“ id”不明确 ,无法解决不明确的问题,但随后出现错误#1060,即重复。

EDIT 2: Here is the code I use to try to get the columns from two separate tables, including the shared registration_id column present in both. 编辑2:这是我用来尝试从两个单独的表中获取列的代码,包括两个表中都存在的共享registration_id列。 I got the code from the post above regarding solving the ambiguous issue: 我从上面的帖子中获得了有关解决歧义问题的代码:

CREATE VIEW access AS
    SELECT fname,lname,email,city,state,country_id,phone,payment_status,event_id,wp_events_attendee.registration_id,wp_events_answer.registration_id,question_id,answer
    FROM wp_events_answer
    JOIN wp_events_attendee ON wp_events_attendee.registration_id = wp_events_answer.registration_id 

It gives me "#1060 - Duplicate column name 'registration_id'". 它给我“#1060-重复的列名'registration_id'”。 Also not sure how to integrate that into the code seen at Create MySQL view using distinct values as columns . 也不确定如何使用不同的值作为列将其集成到在“ 创建MySQL”视图中看到的代码中。 I tried to do a test: 我试图做一个测试:

  CREATE VIEW access AS
SELECT
    fname,lname,email,city,state,country_id,phone,payment_status,event_id,wp_events_attendee.registration_id,wp_events_answer.registration_id,question_id,answer,registration_id,
    MAX(IF(question_id = '1', status, NULL)) FIRST,
    MAX(IF(question_id = '11', status, NULL)) DOB,
FROM wp_events_answer, wp_events_attendee
JOIN wp_events_attendee ON wp_events_attendee.registration_id = wp_events_answer.registration_id 
GROUP BY registration_id;

But got "#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM wp_events_answer, wp_events_attendee GROUP BY registration_id' at line 6". 但是得到了“#1064-您的SQL语法有错误;请查看与您的MySQL服务器版本相对应的手册,以获取正确的语法以在第6行的'FROM wp_events_answer,wp_events_attendee GROUP BY registration_id'附近使用”。 Thanks! 谢谢!

I don't have much knowledge about sql views, but it seems there is a problem in your query when joining tables. 我对sql视图的了解不多,但是似乎在连接表时查询中存在问题。

if you need to join 3 tables, follow the query below. 如果您需要联接3个表,请遵循以下查询。

SELECT `table1`.`column1`,`table2`.`column2` 
FROM `table1` 
JOIN `mapping_table` 
JOIN `table2` 
ON `table1`.`table1_id`=`mapping_table`.`table1_foreign_key` 
AND `mapping_table`.`table2_foreign_key`=`table2`.`table2_id` 
GROUP BY `table1`.`group_column` 

Hope this will help you to create the view you want 希望这会帮助您创建所需的视图

Assuming you know the questions ids in advance this could be achieved with a bunch of UNIONS - http://sqlfiddle.com/#!9/00a0c/8 假设您事先知道了问题ID,则可以使用一堆UNIONS来实现-http://sqlfiddle.com/#!9/ 00a0c/8

I'm afraid I'd need some sample data to answer the second part of your question. 恐怕我需要一些示例数据来回答您问题的第二部分。

UPDATE - I didn't realise that the additional rows were a problem. 更新-我没有意识到额外的行是一个问题。 The following SQL fiddle addresses this issue. 以下SQL提琴解决了这个问题。

http://sqlfiddle.com/#!9/0f8c7/2 http://sqlfiddle.com/#!9/0f8c7/2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM