有人可以告诉我如何回显与另一个表相关的数据吗?
我有一张桌子
tbl_category_products:<
- id_categorys
- category
和其他产品表
tbl_products:
- title
- description
- id_categorys
如何在 PHP 中按类别回显产品列表?
这里有一些代码,但没有成功。我正在尝试但没有成功,这是原始代码表名:
tbl_categorias_noticias- id_categoria_noticia
- 卡特戈里亚
tbl_noticias- id_noticia
- id_categoria_noticia
-蒂图洛
-德里考
-.msg
-nome_arquivo
-传奇
<?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'];
?>
你需要一个连接:
SELECT t1.title, t1.description, t2.category
FORM tbl_products t1
LEFT JOIN tbl_category_products t2 ON t1.id_categorys = t2.id_categorys
像这样的东西
首先: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
}
使用 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
SELECT tp.title, tp.description, tcp.category FORM tbl_products tp,tbl_category_products tcp where tp.id_categorys = tcp.id_categorys
试试这个
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'