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. I'm using phpGrid to output on the front end.
For instance - let's say I have existing column 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):
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?
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
values often have duplicates, but they have individual entry ids.
Cheers and thanks!
EDIT: Create MySQL view using distinct values as columns is the right idea, but I get error #1064 about incorrect syntax.
Also, I'm not sure it solves the second problem of getting the data to align by unique entry 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.
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. 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'". Also not sure how to integrate that into the code seen at Create MySQL view using distinct values as columns. 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". Thanks!
Assuming you know the questions ids in advance this could be achieved with a bunch of 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.