2个具有相同属性的表,如何选择我想要的?

时间:2015-08-11 08:03:26

标签: mysql

我收到了以下代码:

$sql = "SELECT * FROM `products` AS a, products_description, categories_description JOIN products_to_categories AS b WHERE a.products_id=b.products_id AND b.categories_id=2 ORDER BY `a`.`products_date_added` DESC LIMIT 4"; 

mysql_select_db('dewaren_shop');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_assoc($retval))
{
    echo "<a href=/\"{$row['gm_url_keywords']}\"/\"{$row['gm_url_keywords']}\"/><img src=\"{$row['products_image']}\"/><a/>  <br> ".
        "{$row['products_short_description']}  <br> ";
} 
mysql_close($conn);

我从我的mysql中选择了3个表。我需要来自products_description表的URL和来自categories_description表的URL。问题出在两个表中,URL称为“gm_url_keywords”。如何从第一个链接的类别表中选择URL,为第二个链接从products表中选择URL?

1 个答案:

答案 0 :(得分:0)

你需要明确地提及它们。而不是说select *使用列别名(如下所示)明确指定列名称

SELECT p.URL as products_URL,
pd.URL as products_description_URL,
cd.URL as categories_description_URL

FROM `products` p, 
JOIN products_description pd
JOIN categories_description cd