duanran3115 2014-07-14 01:00
浏览 52

如果行存在,如何交叉引用两个sql表然后显示一列的输出?

Please provide some insight, as I am about 99.9% ignorant about working with sql. I have put all columns in parentheses. I want to:

  1. Grab a number (id) from the v14_users table (for the user whose profile page is currently being viewed).

  2. Find the same number (uid) column in the v14_profileurl_urls table

Then, depending on the result of step 2 either:

3a. If (uid) column found, display/print (name) column for same row of v14_profileurl_urls table

or

3b. If (uid) column is not found, display nothing.

The result of 3a would be displayed with something like:

<?php echo $profileurl_urls->name; ?>

Thanks for any help! -Moni

  • 写回答

1条回答 默认 最新

  • dot_0620 2014-07-14 01:11
    关注

    I'm making some assumptions about your database schema but you can use the following if my assumptions are correct:

    SELECT COALESCE(profile.name, 'No Profile') AS Name
    FROM v14_users AS users LEFT JOIN
         v14_profileurl_urls AS profile ON 
             users.id = profile.uid
    

    You can replace the string 'No Profile' with the empty string '' if you really don't want to return anything.

    A coalesce returns the first non-NULL value in the list, or NULL if there are no non-NULL values. Since I've hardwired in the string 'No Profile' as the last item in the list if there is not profile record or the profile name is null then 'No Profile' will always appear.

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭