MYsql使用与连接表相同的字段
我有一个网站需要在不更改任何内容的情况下进行修改,我能做到的唯一方法是添加具有状态字段的 mytable。
现在我有这样的请求
if...{
$filter = "status = 0";
}
SELECT first_name, last_name, position, wage
FROM table1, mytable
WHERE table1.id = mytable.id
$filter
问题是“table1”和“mytable”都有状态,我对此无能为力,因为此过滤器还用于其他 16 个看起来“完全相似”的请求,除了它们使用 table1 - table2、table3、table4 ,table5,table6,...等以及仅用于过滤的状态有人可以帮忙吗?
MySQL 中是否有类似于 php 类中的 $this 的内容,因此它知道我引用 FROM 字段中的表,以便我可以使用 JOIN LEFT 而不是在 FROM 中指定表?
I have site i need to modify without changing anything and only way i can do it is by adding mytable which has status field.
Now i have request like
if...{
$filter = "status = 0";
}
SELECT first_name, last_name, position, wage
FROM table1, mytable
WHERE table1.id = mytable.id
$filter
Problem is that both "table1" and "mytable" have statuses and i cannot do anything about this because this filter used also for 16 other requests looking "exactly similar" except they use instead table1 - table2, table3, table4, table5, table6,... etc and status used only for filtering can someone help?
Is in MySQL something like $this in php class so it knows i reference to table in FROM field so i could use JOIN LEFT instead of specifying table in FROM?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以(为了清楚起见,可能应该)在任何列前添加其表名。您可以在查询的 SELECT 部分和 WHERE 部分中执行此操作。例如:
如果您要动态包含表并希望保持过滤器代码通用,则可以使用 AS 关键字创建别名,因此:
...这样,您就可以切换正在使用的表在
$use_table
中,而不更改任何其他 SQL。You can (and probably should, for clarity) prefix any column with its table name. You may do so both in the SELECT portion of the query as well as the WHERE portion. For example:
If you are going to be dynamically including the tables and want to keep the filter code generic, you can use the AS keyword to create aliases, so:
... that way, you are able to switch which table you are using in
$use_table
without changing any of the other SQL.您可以指定您引用的女巫表的列,例如
"table1"."status"
:You can specify to witch table's column you reference, like
"table1"."status"
: