mysql - SELECT with temporary column with value based on other column -


i have mysql table schema resembles json array below.

[     { "id": 1, "firstname": "charles montgomery", "lastname": "burns", "managerid": 12},     { "id": 2, "firstname": "bart", "lastname": "simpson", "managerid": 1},     { "id": 3, "firstname": "marge", "lastname": "simpson", "managerid": 1},     { "id": 4, "firstname": "lisa", "lastname": "simpson", "managerid": 1},     { "id": 5, "firstname": "maggie", "lastname": "simpson", "managerid": 1},     { "id": 6, "firstname": "homer", "lastname": "simpson", "managerid": 4},     { "id": 7, "firstname": "ned", "lastname": "flanders", "managerid": 4},     { "id": 8, "firstname": "krusty", "lastname": "the clown", "managerid": 2},     { "id": 9, "firstname": "waylon", "lastname": "smithers", "managerid": 2},     { "id": 10, "firstname": "ralph", "lastname": "wiggum", "managerid": 5},     { "id": 11, "firstname": "itchy", "lastname": "", "managerid": 5},     { "id": 12, "firstname": "comic book guy", "lastname": "", "managerid": 4} ] 

managerid field self referencing foreign key id field.

i need make query add column on fly "managername" computed id field hold concatenated firstname , lastname.

below (simplified) query i've tried. question mark placeholder filled mysql driver (in nodejs):

select * ,  (select concat_ws(' ', firstname, lastname) employee managerid = id ) managername employee employee.id = ?; 

i realize where managerid = id place look, don't know condition place match correct values.

can help?

edit in case stumbles upon same thing, i've found helpful article. examples similar.

left join friend. tra use query this:

select      e.*,     concat_ws(' ', m.firstname, m.lastname) managername employee e e.employee.id = ? left join  employee m on e.managerid = m.id; 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -