dtw52353 2015-03-04 12:10
浏览 27
已采纳

如果具有特定值,请选择全部并添加新列

I have a platforms table that has id, name and parent_id and other columns.

Is it possible to write a query that selects all platforms from the table, and if it has a parent_id which is not 0, an extra column gets added which has the parent_name?

Like:

id    name    parent_id
2     pl1     0
3     pl2     0
4     pl3     2

And the desired query would yield:

id    name    parent_id    parent_name
2     pl1     0
3     pl2     0
4     pl3     2            pl1
  • 写回答

1条回答 默认 最新

  • doujin4031 2015-03-04 12:11
    关注

    You can do this using a left join:

    select p.*, pp.name as parent_name
    from platforms p left join
         platforms pp
         on p.parent_id = pp.id;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YOLOv5改进后的结构图
  • ¥15 全志v3s怎么设置高速时钟,使用的荔枝派zero开发板,串口2需要921600的波特率
  • ¥15 关于#单片机#的问题:Lora通讯模块hc-14电路图求内部原理图
  • ¥50 esp32 wroom 32e 芯片解锁
  • ¥15 bywave配置文件写入失败
  • ¥20 基于Simulink的ZPW2000轨道电路仿真
  • ¥15 pycharm找不到在环境装好的opencv-python
  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题