code_junkie

Can I concatenate multiple MySQL rows into one field?

Nov 12th, 2011
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. select hobbies from peoples_hobbies where person_id = 5;
  2.  
  3. shopping
  4. fishing
  5. coding
  6.  
  7. shopping, fishing, coding
  8.  
  9. select person_id, group_concat(hobbies separator ', ')
  10. from peoples_hobbies group by person_id;
  11.  
  12. select group_concat(hobbies separator ', ')
  13. from peoples_hobbies where person_id = 5 group by 'all';
  14.  
  15. <?php
  16. $array = array('shopping', 'fishing', 'coding'); // filling an array
  17. $comma_separated = implode(",", $array); // imploding the array
  18. echo $comma_separated; // lastname,email,phone
  19. ?>
  20.  
  21. <?php
  22.  
  23. $array(explode('lastname,email,phone', $str, 2));
  24.  
  25. ?>
Add Comment
Please, Sign In to add comment