dsshsta97935 2014-05-01 15:06
浏览 84

从1列写2列

I have a table

Registration
╔══════╦══════════════╦══════════════╦═══════════════╗  
║name1 ║   name2      ║ name2's state║   entity_id   ║
╠══════╬══════════════╬══════════════╣═══════════════╣
║  1   ║     92       ║   complete   ║     152       ║
║  92  ║     1        ║   pending    ║     153       ║
╚══════╩══════════════╩══════════════╩═══════════════╝

Is it possible to replace a row's name2 state into "name1's state"? I mean A state 1-->92 is "complete" I know that state 92--->1 is "pending" the question is how to display the same information in the table in one row like in my_table? Is it possible?

my_table
╔══════╦══════════════╦═══════════════╦═══════════════╦═══════════════╗  
║name1 ║     name2    ║ name2's state ║ name1's state ║   entity_id   ║
╠══════╬══════════════╬═══════════════╣═══════════════╣═══════════════╣
║  1   ║     92       ║   complete    ║  pending      ║     152       ║
║  92  ║     1        ║   pending     ║  complete     ║     153       ║
╚══════╩══════════════╩═══════════════╩═══════════════╩═══════════════╝
  • 写回答

1条回答 默认 最新

  • dongyue6199 2014-05-01 15:25
    关注

    What you will want to do is something like this

    SELECT
     t1.name1,
     t1.name2,
     t1.name2state as name1state,
     t2.name2state as name2state,
     t1.entity_id
    FROM
     names t1
    JOIN
     names t2
    ON
     t1.name2 = t2.name1;
    ORDER BY
     t1.name1;
    

    here is an SQL fiddle you can play with.

    http://sqlfiddle.com/#!2/79222/4

    of course this just gives you the data you asked for and an example of a join, you will need to appy this and probably change it in a way to suit your needs.

    Edit:

    JOIN keyword for a standard join, this JOIN clause will only show records where the ON statement is true. The ON clause is denoting which two columns are being compared for the JOIN. so in the above SQL statement you are saying you want to SELECT your data from TABLE 1 that has TABLE 2 JOINED onto TABLE 1 where TABLE1's column name2 = TABLE2's column name1. There are many other types of JOINs you can use to manipulate that data. Look at this Stackoverflow thread to see all of the types of JOINs you can use and what they do.

    What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀