MYsql使用与连接表相同的字段

发布于 2024-12-07 05:48:40 字数 525 浏览 0 评论 0原文

我有一个网站需要在不更改任何内容的情况下进行修改,我能做到的唯一方法是添加具有状态字段的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

古镇旧梦 2024-12-14 05:48:40

您可以(为了清楚起见,可能应该)在任何列前添加其表名。您可以在查询的 SELECT 部分和 WHERE 部分中执行此操作。例如:

SELECT 
    table1.first_name, 
    table1.last_name, 
    mytable.position, 
    mytable.wage 
FROM 
    table1, 
    mytable 
WHERE 
    table1.id = mytable.id AND 
    table1.status = "0"

如果您要动态包含表并希望保持过滤器代码通用,则可以使用 AS 关键字创建别名,因此:

$use_table = 'table1';
$sql = '
    SELECT 
        filter_table.first_name, 
        filter_table.last_name, 
        mytable.position, 
        mytable.wage 
    FROM 
        '.$use_table'. AS filter_table, 
        mytable 
    WHERE 
        filter_table.id = mytable.id AND 
        filter_table.status = "0"
';

...这样,您就可以切换正在使用的表在 $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:

SELECT 
    table1.first_name, 
    table1.last_name, 
    mytable.position, 
    mytable.wage 
FROM 
    table1, 
    mytable 
WHERE 
    table1.id = mytable.id AND 
    table1.status = "0"

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:

$use_table = 'table1';
$sql = '
    SELECT 
        filter_table.first_name, 
        filter_table.last_name, 
        mytable.position, 
        mytable.wage 
    FROM 
        '.$use_table'. AS filter_table, 
        mytable 
    WHERE 
        filter_table.id = mytable.id AND 
        filter_table.status = "0"
';

... that way, you are able to switch which table you are using in $use_table without changing any of the other SQL.

往事随风而去 2024-12-14 05:48:40

您可以指定您引用的女巫表的列,例如 "table1"."status"

SELECT first_name, last_name, position, wage 
FROM table1, mytable
WHERE table1.id = mytable.id AND "table1"."status" = 0

You can specify to witch table's column you reference, like "table1"."status" :

SELECT first_name, last_name, position, wage 
FROM table1, mytable
WHERE table1.id = mytable.id AND "table1"."status" = 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文