开发者

Need help, finding identical values from two table then out putting the results if there is a match

Hi am am relatively new to PHP and mysql and would really appreciate any help… pleeeeease!

I think it is simple but I am having trouble with it. I have two tables

Table 1: product_color

-- Id
-- base_color
-- base_color_name
-- red
-- green
-- blue

Table 2: artwork

-- id
-- art_name
-- product color

The "base_colo开发者_如何学Pythonr" in the Product_color Table, and the "product_color" in the Artwork table have identical values, where the Artwork table has a many to one relationship to the Product Color table.

What I need to do is compare the two tables and IF the value in the Artwork table matches any the identical value in the Product color table then I want to echo the data from the red, green and blue fields of that matching row in the product_table.

I sort of got the mysql statement. But for the life of me I cannot figure out how to retrieve the "Red, Green, and Blue" values from the "product_color" table :(

Here is the mySQL statement:

SELECT * FROM product_color ON artwork.product_color = product_color.base_color

Any help would be SOOOOO appreciated!!


You're close:

SELECT * FROM product_color JOIN artwork ON product_color=base_color

Since you asked for a complete example. Here it is using native functions. (I recommend using PDO if you can but it's a little more involved)

$res = mysql_query( "SELECT * FROM product_color JOIN artwork ON product_color=base_color" );

// check for errors
if( !$res ) {
     die( mysql_error() ); 
}

// now loop through each of the rows returned from the query.
while( $row = mysql_fetch_assoc($res) ) {
     // the row is available as an associative array by column name
     // so if you want to echo product_color:
     echo $row['product_color'];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜