douyu2817 2015-09-29 15:01
浏览 100
已采纳

在这个mysql语句中,“s。”和“n”是什么意思/做什么?

I'm currently working on a project that requires me look at someone elses code, i'm still new to some of this stuff and there is something that I dont quite understand so was hoping someone could shed some light on what the code is actually doing or what it means?

Here is the statement in question:

select s.* from $tableA n, $tableB s where
            n.id='$send' and
            n.status='$status' and
            n.field=s.id";

I understand that down to basics this statement is getting all of the fields from tableA and tableB im just unsure what the s. does or what the n does in this statement? are they simply there as identifiers or am I completely wrong in this manner? I am happy to provide more information if it is necessary.

  • 写回答

1条回答 默认 最新

  • dtf24224 2015-09-29 15:14
    关注

    They are called SQL Table Aliases and are basically temporary names which you give to the tables in order to have better readability when you use the table names to specify a column.

    In your example

    SELECT s.* FROM $tableA n, $tableB s
    WHERE  n.id='$send'
           AND n.status='$status'
           AND n.field=s.id ;
    

    is the same as

    SELECT $tableB.* FROM $tableA, $tableB
    WHERE  $tableA.id='$send'
           AND $tableA.status = '$status'
           AND $tableA.field = $tableB.id ;
    

    but obviously it's easier to read.

    The table aliases are even more useful when you join more tables and are absolutely a must when you make self joins.

    Syntax note:

    You may or you may not use the AS keyword when alias a table.

    SELECT table_name AS alias
    

    is the same as

    SELECT table_name alias
    

    and although it's longer sometimes it leads to a better readability (for example in a large and messy query the big AS is easier to spot :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 机器学习简单问题解决
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写