2015-07-22 49 views
2

我是Mysql中的新成員。我有多個表,我想加入他們所有人。我爲此使用INNER JOIN。如何在SQL INNER JOIN中使用AS?

"SELECT * 
FROM table 
       INNER JOIN table2 
       ON table.client_id = table2.id 

      WHERE table2.id= 113 
      ORDER BY table.id DESC 
      LIMIT 1 "; 

這裏我面臨一個問題,我在每個表中有一個列名稱標題。我想在我的命令中使用AS。就像在Outlook表格中,列的名稱是TITLE,我想將其用作outlook_title。怎麼可能?

我想用回聲打印這樣

echo '<h1>' .$row["client"]. '</h1>' ; 
echo '<h1>' .$row["name"]. '</h1>' ; 
echo '<h1>' .$row["name"]. '</h1>' ; 
echo '<h1>' .$row["ation_title"]. '</h1>' ; 
echo '<h1>' .$row["look_title"]. '</h1>' ; 

數據請幫我

+0

使用'og_outlooks.title',只是前面加上表名稱的列給別名。 – Mihai

+0

col名稱標題可能會略有不同,以致於你在 – Drew

+0

@Mihai請參考我的語法 – sunny

回答

2

你說的每一個表,我知道什麼:>

在您的休閒添加其他列。

SELECT r.title as rtitle,c.title as ctitle, 
t.title as ttitle,a.title as atitle,o.title as otitle 
FROM og_ratings r 
INNER JOIN og_companies c 
ON r.client_id = c.id 
INNER JOIN og_rating_types t 
ON r.rating_type_id = t.id 
INNER JOIN og_actions a 
ON r.pacra_action = a.id 
INNER JOIN og_outlooks o 
ON r.pacra_outlook = o.id 
WHERE c.id= 113 
ORDER BY r.id DESC 
LIMIT 1 
1

您可以使用,以你的列,也爲subquery結果在選擇

SELECT s.title as stitle,p.title as ptitle 
FROM og_ratings s 
INNER JOIN og_companies p 
ON s.client_id = p.id 
WHERE s.id= 115