2015-07-11 175 views
-1

有两个mysql表games_serverorders_order。只有当该表中的值id等于第二个表orders_order的值server_id时,我需要从第一个表中选择名为full_address的值。如果一个值等于另一个表中的值,请从其中一个表中选择一个值

但不只是任何价值。它必须是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不太好。请帮忙。

回答

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 
+0

可能是一个想法,在继续之前阅读一本书或基本教程。 – Strawberry

-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

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"); 
+0

你太棒了!谢谢!完美工作。 –

+0

很高兴帮助。干杯。 –

相关问题