Jump to content

Please help with query


lingo5

Recommended Posts

hi,

I have a table called "products" where i store product info including id_family.

I have another table called "families" where I store the family_name and id_family.

 

This is how I get all my products from the DB:

 

mysql_select_db($database_MySQLconnect, $MySQLconnect);
$query_products_RS = "SELECT * FROM t_products ORDER BY product_titulo_esp ASC";
$query_limit_products_RS = sprintf("%s LIMIT %d, %d", $query_products_RS, $startRow_products_RS, $maxRows_products_RS);
$productsRS = mysql_query($query_limit_productsRS, $MySQLconnect) or die(mysql_error());
$row_products_RS = mysql_fetch_assoc($products_RS);

 

...this works fine.

 

Whay i need to do now is to get the family_name from the table "families" for each product based on the id_family column from the "products" table.....

 

do I make sense?....thanks!!

Link to comment
https://forums.phpfreaks.com/topic/272694-please-help-with-query/
Share on other sites

What you need to do is called a join. Example:

 

$q = "SELECT 
product_id,
product_title,
family_name,
prod.id_family,
fam.id_family 
FROM products 
LEFT JOIN family 
ON prod.family_id = prod.family_id";

 

I am using an alias with id_family because I am assuming that the id names are the same in both tables.

What you need to do is called a join. Example:

 

$q = "SELECT
product_id,
product_title,
family_name,
prod.id_family,
fam.id_family
FROM products
LEFT JOIN family
ON prod.family_id = prod.family_id";

 

I am using an alias with id_family because I am assuming that the id names are the same in both tables.

did you even read that before you posted it?

 

$q = "SELECT

product_id,

product_title,

family_name,

prod.id_family, <<--- prod is not a table

fam.id_family <<---fam is also not a table. Why would you want the same information twice anyway?

FROM products <<-----no alias applied to the table name

LEFT JOIN family <<-----as above

ON prod.family_id = prod.family_id"; <<----your linking the same field in the same table to the same field in the same table!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.