2013-03-07 56 views
0

好吧,這是嚴酷的,Mysql的選擇多表獲得值與同一列

不管怎麼說,我修改了我的問題,在這裏提出的代碼的例子。

我想要做的是,我需要得到贈品表的列標題值競爭列標題值

$query = "SELECT giveaway_table.title, competition.title FROM giveaway_table, competition WHERE giveaway_table.status=competition.status"; 
$result = db_query($query)->fetchObject(); 

如何檢索值?

當我用這個

echo $result->title; 

它只能隨聲附和競爭的標題值。

如何檢索贈品表的標題欄值?

我用這個

$result->title[0] 

那隻能說明競爭的標題值的名稱的第一個字母。

任何幫助,將不勝感激。 :)

回答

2

使用不同的別名:

$query = "SELECT giveaway_table.title as gtitle, competition.title as ctitle FROM giveaway_table, competition WHERE giveaway_table.status=competition.status"; 
2

你可以使用別名來清理歧義:

$query = "SELECT giveaway_table.title AS giveaway_title, competition.title AS competition_title FROM giveaway_table, competition WHERE giveaway_table.status=competition.status"; 
$result = db_query($query)->fetchObject(); 

然後附和:

echo $result->competition_title; 
echo $result->giveaway_title; 
2

使用別名

SELECT 
    giveaway_table.title AS GiveAwayTitle, 
    competition.title AS CompetitionTitle 
FROM giveaway_table, 
    competition 
WHERE giveaway_table.status = competition.status 

用php

echo $result->GiveAwayTitle; 
echo $result->CompetitionTitle;