简体   繁体   English

SQL查询中的变量

[英]Variables in SQL queries

In the below query I noticed these named variables wposts, wpostmeta and was wondering how they work and what are they called (MySQL variables) so I can find out more information about using them in the MySQL docs. 在下面的查询中,我注意到了这些命名变量wposts, wpostmeta ,并想知道它们的工作方式以及它们的名称(MySQL变量),因此我可以在MySQL文档中找到有关使用它们的更多信息。

Is it a shorthand so you don't have to type $wpdb->postmeta every time or is there more to this? 这是一种简写形式,因此您不必每次都键入$wpdb->postmeta还是还有更多呢? Also, I don't understand the SELECT wposts.* there is no Wordpress table called wposts so what are you selecting from? 另外,我不了解SELECT wposts.*没有称为wposts的Wordpress表,因此您从中选择什么?

https://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query https://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query

$querystr = "
        SELECT wposts.* 
        FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
        WHERE wposts.ID = wpostmeta.post_id 
        AND wpostmeta.meta_key = 'custom-key' 
        AND wposts.post_type = 'page' 
        ORDER BY wpostmeta.meta_value DESC
 ";

They are table aliases, and provide (generally) a short-hand way of referring to a table in a query. 它们是表别名,提供(通常)在查询中引用表的简便方法。 In your query the table whose name was $wpdb->posts is now referred to as wposts , and the table called $wpdb->postmeta is now referred to as wpostmeta . 在您的查询中,名称为$wpdb->posts表现在称为wposts ,而名为$wpdb->postmeta表现在称为wpostmeta Note that once you have declared an alias, you must refer to the table using that alias, hence in your query you have references to wposts.* , wpostmeta.meta_value etc. 请注意,一旦声明了别名, 就必须使用该别名引用表,因此在查询wposts.*引用wposts.*wpostmeta.meta_value等。

Note that you can also have column aliases, for example: 请注意,您还可以具有列别名,例如:

SELECT SUM(x) AS total FROM t1

Also note that the AS keyword I have shown in the column select above is optional and can be used for table and column aliases. 还要注意,我在上面的选择列中显示的AS关键字是可选的,可用于表和列的别名。

You can read more about aliases here . 您可以在此处阅读有关别名的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM