来自不同表格的查询如何加入建议PLease(PHP)(SQL)

问题描述:

我需要关于JOIN表获得帮助,从其他表中获取类别名称&作者姓名。来自不同表格的查询如何加入建议PLease(PHP)(SQL)

的文章的网站我有三个表

  • 文章
  • 类别
  • 用户

文章表结构:

id 
title 
slug 
content 
category 
author 
date 

我保存类别ID在文章表用户ID与每篇文章

同时显示文章我用这个类别: (只是一些想法草案不完整的代码)

localhost/testsite/category.php?cat=category-slug 

$catslug = ($_GET["cat"]); 

//GET CAT ID from category table from slug 

$qry = "SELECT * from category WHERE slug='$catslug'; 
$res = mysql_query($qry) or die("Error in Query"); 
$ans = mysql_fetch_array($res); 
$cid = $ans['id']; 
$catname = $ans['title']; 

<h2><?php echo $catname; ?></h2> 

//after getting cat-id query to get article of that ID 

$aqry = select * from article where category=$cid ORDER BY date"; 
$ares = mysql_query($aqry) or die("Error in Query"); 
$aans = mysql_fetch_array($ares)) 
$aid = $aans['author']; 

//then another query to get author name from users 
select username from users where id=$aid 

我第m个学习PHP &没有多少熟悉JOIN声明&合并查询 需要帮助/建议hw我可以通过JOIN表实现相同的操作,而不是使用不同的查询来从表中获取catname和作者姓名。

感谢

这应该做

SELECT distinct c.title, U.Username 
from category C join article A on A.category=C.id 
join users U on U.id=A.author 
WHERE C.slug= :catslug 
+0

请尝试解释此查询到我 我知道加入,但没有太多关于不同 – Vehlad 2012-03-22 13:27:43

+0

不同将获得一个唯一的字段出现时消除重复。就像你会和团队一样。说实话,我不记得mysql是否使用这种语法,或者这些字段必须放在parentesis中。 – Iridio 2012-03-22 13:37:24

+0

非常感谢方向我有了一些变化:))得到它 去除不同,因为我想从该类别 还删除U.id = A.author条件获取所有的文章我想列出不但任何单个作者的所有文章从用户表中选择作者姓名。 – Vehlad 2012-03-22 17:13:39

假设类别ID是你想做的事,文章表外JEY

尝试此查询

select username from users 
where id in (
select author from category cat 
inner join article art 
on cat.category_id =art.category_id 
where cat.slug= $cat 
order by art.date desc) 
+0

类别ID,作者ID没有外键在文章表 hmmmmmm 我应该建立外键的文章,以加快进程。 我将与此 – Vehlad 2012-03-22 13:26:05

$q = "SELECT article.* 
     FROM article 
      LEFT JOIN category 
      ON article.category = category.ID 
      LEFT JOIN users 
      ON article.author = users.ID 
     WHERE article.slug = :slug"; 

:slug在这里,你会(如果你使用PDO绑定)插入$_GET['slug']

+0

我试图 '$ q =“选择文章。*从物品 \t \t \t LEFT JOIN类别 \t \t \t上articles.category = categories.id \t \t \t LEFT JOIN用户 \t \t \t on articles.author = users。ID \t \t \t WHERE categories.slug =“$猫”“;' 我改变WHERE category.slug因为我想通过$ _GET通过塞从单一的品类查询物品$猫 这导致在查询错误 – Vehlad 2012-03-22 16:41:52

首先,你应该写一个存储过程......但这里是你如何度日的categoryID同时包含类名和用户名的文章。

如下表结构:

article 
--------- 
articleID <- primary key 
title 
slug 
content 
categoryID <- foreign key 
userID <- foreign key 
createdDT 

category 
-------- 
categoryID <- primary key 
categoryName 

user 
-------- 
userID <- primary key 
userName 

下面介绍如何使用连接到编写查询:

SELECT a.title, a.slug, a.content, a.createdDT, c.categoryName, u.userName 

FROM dbo.article a 

JOIN dbo.category c on a.categoryID = c.categoryID 
JOIN dbo.user u on a.userID = u.userID 

WHERE a.categoryID = @categoryID 

下面是来自微软的文章连接 - http://msdn.microsoft.com/en-us/library/ms191517.aspx

+0

这里@CategoryID是$ _GET的值? – Vehlad 2012-03-22 16:30:30

+0

如果你创建了一个存储过程,@CategoryID将是输入参数之一,如果你使用内联查询,那么是的。 – 2012-03-22 17:16:30

你可以做一些与SQL请求不同的事情。你可以做一些加入您的SQL请求,但你也可以做另一个SELECT语句在你的要求就像

SELECT SOMETHINGS FROM A_TABLE WHERE (SELECT ANOTHER_THING FROM ANOTHER_TABLE WHERE VALUE =1 

但是,如果你在你的第二个请求

1分的结果。如果你想这种说法只会工作连接两个表一定的价值,你必须做这样

SELECT something FROM a_table AS alias1 JOIN another_table AS alias2 
ON alias1.value1 = alias2.value1 and ..... 

您可以比较所有你从一个表要到另一个值,你也可以加入两个以上的表和ON关键词不是通过sql语法请求的,你可以只做

SELECT * FROM table_1 join table_2 

,它会告诉你从两个表中的所有thje数据,但可以肯定,这是你想要这种小的查询可以有一些大的成果,减缓您的应用程序

您可以读取数据更多有http://dev.mysql.com/doc/refman/5.0/en/join.html