php - 将单个表的两行与除一个之外的所有重复值连接起来?

标签 php mysql codeigniter

我用谷歌搜索了很多,但没有找到任何合适的解决方案。我是开发人员,对 sql 查询没有深入的了解。请帮助我。

该表是由两个表 user & location 与外键 location_id 的 LEFT JOIN 生成的。

getting this

现在我希望它像下面这样..

expecting this

最佳答案

如果一个用户最多只有两个地址,那么您可以尝试像这样的子查询:

SELECT 
  u.user_id,
  u.full_name,
  u.user_type, 
  (SELECT name FROM location WHERE id=u.location_id LIMIT 1) location,
  (SELECT name FROM location WHERE id=u.location_id LIMIT 1 OFFSET 1) location_something
FROM user u
...

如果只找到一个地址,location_something 将为空。

关于php - 将单个表的两行与除一个之外的所有重复值连接起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35380293/

相关文章:

PHP MYSQL 消除表中的逗号

php - 动态 <title> 与 PHP 和 MySQL 错误

mysql - SQL:按多个字段分组

php - CodeIgniter View 中应包含多少代码?

mysql - codeigniter链查询关键字问题

php - Symfony 表单错误 : "Expected argument of type "Doctrine\ORM\QueryBuilder", "Doctrine\ORM\Query"给出”

php - 使用 PHP 和 MySQL 自动发送电子邮件?

php - 在 MySQL 数据库中搜索模式中的字符串

php - 我有一列包含逗号分隔值如何使用数组搜索这些值

Codeigniter:下载文件并重定向