简体   繁体   中英

mysql select all from table but change column name of only one

I need to select a table which contains id, name and some more. Now i want to select everything, but just change the column name 'name' to 'user_name'. How can i do this without selecting and declaring everything? I tried it like this, but this is not working.

return $query
->select(
  'user.*',
  'user.name as user_name'
)

Your issue starts from your joins. Because you join without aliasing, then the second one overwrites the first. I give you a sample query to show how you can alias your joined table, and thus alias the 'name' from the second table. Sample code:

$query = DB::table('users')
->leftjoin('users as child', 'users.id', '=', 'users.parent_id')
->select('users.*', 'child.name as child_name')
->get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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