如果值等于另一个表中的值,则从其中一个表中选择一个值

时间:2015-07-11 07:53:08

标签: php mysql

有两个mysql表games_serverorders_order。只有当此表中的值full_address等于第二个表id的值server_id时,我才需要从第一个表中选择名为orders_order的值。

但不仅仅是任何价值。它必须是WHERE service_type = 'be_first' AND status = 'running'

我在这里阅读了可能的解决方案并尝试了类似的方法:

SELECT server_address
FROM games_server
WHERE id IN
    SELECT server_id
    FROM orders_order
    WHERE service_type = 'be_first'
      AND status = 'running'
ORDER BY updated DESC

但它不起作用。我对mysql不好。请帮忙。

3 个答案:

答案 0 :(得分:0)

SELECT a.thing you want to select
  FROM table you want to select from AS a
  JOIN the other table AS b
    ON b.some column = a.some column
 WHERE some criterion is met
   AND another criterion is met

答案 1 :(得分:0)

尝试:

mysql_query("SELECT a.full_address FROM games_server AS a JOIN orders_order AS b ON b.server_id = a.id WHERE service_type = 'be_first' AND status = 'running'");

然后按以下方式添加订单:

mysql_query("SELECT a.full_address FROM games_server AS a JOIN orders_order AS b ON b.server_id = a.id WHERE service_type = 'be_first' AND status = 'running' ORDER BY column_name");

答案 2 :(得分:-1)

mysql_query("SELECT a.'full_address' FROM 'games_server' AS 'a' JOIN 'orders_order' AS 'b' ON b.'server_id' = a.'id' WHERE service_type = 'be_first' AND status = 'running');

是您拥有的代码,我非常确定它会引发错误。请尝试删除AS关键字,使其如下所示:

mysql_query("SELECT a.'full_address' FROM 'games_server' a JOIN 'orders_order' b ON b.'server_id' = a.'id' WHERE service_type = 'be_first' AND status = 'running');

尝试阅读mysql网站上的以下文章:https://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html