douting1871 2013-04-22 04:40
浏览 22

如何获取表中的行?

I have a table with the following columns:

User_id, Key, Value

In this table are records such as:

1, "first name", "Bob"  
2, "first name", "Mike"  
1, "last name", "Jones"  
2, "last name", "Carter"

I want to get the user_id for a specific first and last name pair. How can I get a query to do this? For example, I want to get 1 if I query for Bob Jones

  • 写回答

2条回答 默认 最新

  • dtx6087 2013-04-22 04:43
    关注

    Join the table to itself:

    SELECT t1.User_id
    FROM myTable t1
    JOIN myTable t2 ON t1.User_Id = t2.UserId
    WHERE t1.Key = 'first name' AND t2.Key = 'last name'
    AND t1.Value = "Bob" AND t2.Value = "Jones"
    

    Or with following concatenated condition:

    SELECT t1.User_id
    FROM myTable t1
    JOIN myTable t2 ON t1.User_Id = t2.UserId
    WHERE t1.Key = 'first name' AND t2.Key = 'last name'
    AND (t1.Value + ' ' + t2.Value) = "Bob Jones"
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大