0

有人可以告诉我如何回显与另一个表相关的数据吗?

我有一张桌子

tbl_category_products:<
- id_categorys
- category

和其他产品表

tbl_products:
- title
- description
- id_categorys

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

这是一些代码,但没有成功。我正在尝试但没有成功,这是原始代码表名称:tbl_categorias_noticias
- id_categoria_noticias
- 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'];

  ?>
4

5 回答 5

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
于 2012-06-21T10:39:23.833 回答
1

像这样的东西

首先:mysql连接

$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
}
于 2012-06-21T10:44:49.010 回答
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'
于 2012-06-21T10:49:39.510 回答
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
于 2012-06-21T10:41:23.113 回答
0
SELECT tp.title, tp.description, tcp.category FORM tbl_products tp,tbl_category_products tcp where tp.id_categorys = tcp.id_categorys
于 2012-06-21T10:42:57.880 回答