按类别回收产品列表

时间:2012-06-21 10:35:56

标签: php mysql sql

有人能告诉我如何回显与另一张桌子有关的数据吗?

我有一张桌子

tbl_category_products:<
- id_categorys
- category

和其他产品表

tbl_products:
- title
- description
- id_categorys

我如何在PHP中按类别回显产品列表?

这是一些代码,但没有成功。 我尝试但没有成功,这是原始代码 表名: tbl_categorias_noticias
- id_categoria_noticia
- categoria

tbl_noticias
- id_noticia
- id_categoria_noticia
-titulo
-decricao
-msg
-nome_arquivo
-legenda

<?php
 $categoryId = 1; 
  $sql_visualizar = mysql_query("SELECT * FROM tbl_noticias AS t1 JOIN tbl_categorias_noticias c
ON c.id_categoria_noticia=t1.id_categoria_noticia WHERE id_categoria_noticia = {$categoryId}");
while($linha = mysql_fetch_array($sql_visualizar)){
$titulo = $linha ['titulo'];
$descricao = $linha['descricao'];
$msg = $linha['msg'];
$legenda = $linha['legenda'];
$nome_arquivo = $linha['nome_arquivo'];
$id_noticia = $linha['id_noticia'];

  ?>

5 个答案:

答案 0 :(得分:1)

您需要加入:

SELECT t1.title, t1.description, t2.category
FORM tbl_products t1
LEFT JOIN tbl_category_products t2 ON t1.id_categorys = t2.id_categorys

答案 1 :(得分:1)

像这样的东西

首先:mysql connect

$categoryId = 1;    
$query = "SELECT t1.* FROM tbl_products AS t1 JOIN tbl_category_products c
ON c.id_categorys=t1.id_categorys WHERE id_categorys = {$categoryId}";
$result = mysql_query($query);

while($r = mysql_fetch_assoc($result)) {
    //show your products
}

答案 2 :(得分:0)

使用JOIN查询两个表

SELECT p.title, p.decription, c.category
FROM tbl_products p
JOIN tbl_category_products c
ON c.id_categorys=p.id_categorys
ORDER BY c.category ASC

答案 3 :(得分:0)

SELECT tp.title, tp.description, tcp.category FORM tbl_products tp,tbl_category_products tcp where tp.id_categorys = tcp.id_categorys

答案 4 :(得分:0)

试试这个

SELECT t1.title, t1.description, t2.category
FORM tbl_products t1
JOIN
tbl_category_products t2 
ON t1.id_categorys = t2.id_categorys
WHERE t1.id_categorys = 'myCategory'