如何在 mysql 中连接地址和状态

how to concate address with state in mysql

我正在尝试将所有地址与状态连接起来,假设我有相同状态和不同地址的行,所以我想 运行 查询会带来一些东西

    state      |  Address
    Maharastra    ABC,DEF,XYZ
    DELHI      |  WRU

目前我的table是

  username | state      | Address
  abc         Maharastra   ABC
  abc         Maharastra   DEF
  abc         Maharastra   XYZ
  abc         DELHI        WRU
  def         Maharastra   ABC
  def         Maharastra   OVU
  def         GOA          IKL
  def         DELHI        WRU

我尝试了什么

   SELECT address,state from location where username='abc' GROUP BY state

我的输出是

   address  state   
   ABC      Maharastra
   WRU      DELHI

GROUP_CONCAT()就是你要找的

SELECT STATE, GROUP_CONCAT(Address ORDER BY Address SEPARATOR ',') 
FROM location where username='abc'
GROUP BY STATE