MYSQL链接,加入两个表

时间:2015-03-04 14:12:16

标签: php mysql

我使用MYS和使用JOIN的经验非常有限,现在遇到了障碍。

我有一个包含产品列表的主表。每种产品都有多种属性,如颜色,大小,品牌(每个产品的列/字段)。而不是为每个存储文本我使用number_id。

 1. Yellow
 2. Blue
 3. Sony
 4. Microsoft
 5. Green

所以我想要一个记录

Name, Colour, Brand

Controller, 1, 3 = Controler, Yellow, Sony.

SELECT
product_data.id                         AS id,
product_data.title                      AS title,
product_data.compatability              AS compatability,
product_data.release_date               AS release_date,
product_data.colour                     AS colour,
product_data.brand                      AS brand,
product_data.type_accessory             AS type_accessory,
product_data.created_userinfo           AS created_userinfo,
product_data.created_timestamp          AS created_timestamp

FROM product_data
INNER JOIN product_attributes
</code>

但是我现在还不确定如何更改“AS”颜色&#39;等于文字&#39; YELLOW&#39;而不是&#39; 1&#39;

<code>
[id] => 13413
[title] => Controler
[compatability] => 
[release_date] => 2016
[colour] => 1
[brand] => 3
[type_accessory] => 54
[created_userinfo] => TEST-USER
[created_timestamp] => 1425476393
[category_main_id] => 4
[category_main_description] => Video Game Accessories
[category_sub_id] => 43
[category_sub_description] => Microsoft Xbox One
[colour1] => 1
</code>

我做了以下更改,但我在颜色中得到空值。      SELECT product_data.title AS title, colours.name AS colour

FROM product_data
    INNER JOIN colours ON colours.id_colour = product_data.colour 

WHERE
    product_data.created_userinfo = 'TEST-USER'

ORDER BY
    title
</code>


<code>
Array
(
[0] => Array
    (
        [title] => Controller
        [colour] => 
        [category_main] => 
        [category_sub] => 
        [brand] => 
    )

[1] => Array
    (
        [title] => Controller Wifi
        [colour] => 
        [category_main] => 
        [category_sub] => 
        [brand] => 
    )
)
</code>

2 个答案:

答案 0 :(得分:1)

您应该将属性保存在每个不同的表中,例如:

CREATE TABLE colours(
   id_colour int PRIMARY KEY,
   name varchar(255)
);

在名为color的字段的主表product_data中,您应该保留颜色的id。然后你可以像这样查询:

SELECT 

   product_data.id                         AS id,
   product_data.title                      AS title,
   product_data.compatability              AS compatability,
   product_data.release_date               AS release_date,
   colours.name                             AS colour

   FROM product_data
   INNER JOIN colours ON colours.id_colour = product_data.colour 

PS。如果列名中没有冲突,您可以在查询中保留表名。

SELECT 

   id                         AS id,
   title                      AS title,
   compatability              AS compatability,
   release_date               AS release_date,
   name                       AS colour

FROM product_data
INNER JOIN colours ON colours.id_colour = product_data.colour 

编辑:

请记住,product_data.id_colour和colours.id_colour字段必须具有相同的数据类型 - &gt;中间体

答案 1 :(得分:0)

您必须定义一个名为颜色的表格,列 ID 名称,然后您的查询将如下所示:

SELECT
    product_data.id,
    product_data.title,
    colour.name AS colour
FROM product_data
INNER JOIN colour ON colour.id = product_data.colour