mysql - 重新映射 MYSQL 中的行

标签 mysql remap

我有这样的表:

uid | fid | value
1   | 1   | nameOf1
1   | 2   | surnameOf1
1   | 3   | countryOf1
2   | 1   | nameOf2
2   | 2   | surnameOf2

并且需要像这样转换/选择它:

uid | name    | surname    | country
1   | nameOf1 | surnameOf1 | countryOf1
2   | nameOf2 | surnameOf2 | ..

最佳答案

尝试如下:

SELECT t1.uid, t2.value AS name, t3.value AS surname, t4.value AS country FROM table t1
  LEFT JOIN table t2 on t2.uid = t1.uid AND t2.fid = 1
  LEFT JOIN table t3 on t3.uid = t1.uid AND t3.fid = 2
  LEFT JOIN table t4 on t4.uid = t1.uid AND t4.fid = 3
  GROUP BY t1.uid;

或者:

SELECT DISTINCT t1.uid, t2.value AS name, t3.value AS surname, t4.value AS country FROM table t1
  LEFT JOIN table t2 on t2.uid = t1.uid AND t2.fid = 1
  LEFT JOIN table t3 on t3.uid = t1.uid AND t3.fid = 2
  LEFT JOIN table t4 on t4.uid = t1.uid AND t4.fid = 3;

关于mysql - 重新映射 MYSQL 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20032446/

相关文章:

python - OpenCV 重映射插值错误?

keyboard-shortcuts - 如何在 Chrome OS 中重新映射键?

mysql - 连接 2 个表的 SQL 查询

php - 需要帮助分解 PHP 脚本/超过 max_execution_time

mysql - 有什么方法可以知道 mysql 中的 select 语句读取了多少行?

php - 如何从php中的数据库中获取最大值?

macros - 两个独立的键盘,两个独立的空格键

Mysql更新多个字段

linux - 有没有办法在 macbook pro 上交换 linux 中的 fn(功能)和控制键?

c++ - 如何使用 LowLevelKeyboardProc 在 C++ 中重新映射键盘键?