SQL 语句(内连接?)
我有两个表 wnews 和categories。
我想按 id 打印类别名称,
例如 wnews.category (是整数)和类别(id,名称),其中 wnews.category = cateroies.id 和打印名称
我做了一些东西,但失败了......
$query = mysql_query("SELECT wnews.id, wnews.torrentid, wnews.title,
wnews.img, wnews.added, wnews.category,
wnews.genre, wnews.uploader, wnews.description,
categories.id, categories.name
FROM wnews
INNER JOIN categories
ON wnews.category=categories.id
ORDER BY wnews.added DESC")
or sqlerr();
while ($arr = mysql_fetch_assoc($query))
{ printNews($arr["wnews.torrentid"], $arr["wnews.title"],
$arr["wnews.img"], $arr["wnews.added"],
$arr["categories.name"], $arr["wnews.genre"],
$arr["wnews.uploader"], $arr["wnews.description"]);
}
感谢您的时间。
解决了。
$query = mysql_query("SELECT wnews.id, wnews.torrentid, wnews.title, wnews.img, wnews.added,
wnews.category, wnews.genre, wnews.uploader, wnews.description, categories.id, categories.name AS cat_name FROM wnews
LEFT JOIN categories ON wnews.category = categories.id
ORDER BY wnews.added DESC") or die(mysql_error());
while ($arr = mysql_fetch_assoc($query))
{
printNews($arr["torrentid"], $arr["title"], $arr["img"], $arr["added"], $arr["cat_name"], $arr["genre"], $arr["uploader"], $arr["description"]);
}
I have two tables wnews and categories.
I wanna print the names of categories by id
something like wnews.category (is an integer) and categories (id, name) where
wnews.category = cateroies.id and print name
I made something but I failde ...
$query = mysql_query("SELECT wnews.id, wnews.torrentid, wnews.title,
wnews.img, wnews.added, wnews.category,
wnews.genre, wnews.uploader, wnews.description,
categories.id, categories.name
FROM wnews
INNER JOIN categories
ON wnews.category=categories.id
ORDER BY wnews.added DESC")
or sqlerr();
while ($arr = mysql_fetch_assoc($query))
{ printNews($arr["wnews.torrentid"], $arr["wnews.title"],
$arr["wnews.img"], $arr["wnews.added"],
$arr["categories.name"], $arr["wnews.genre"],
$arr["wnews.uploader"], $arr["wnews.description"]);
}
Thanks for your time.
Solved.
$query = mysql_query("SELECT wnews.id, wnews.torrentid, wnews.title, wnews.img, wnews.added,
wnews.category, wnews.genre, wnews.uploader, wnews.description, categories.id, categories.name AS cat_name FROM wnews
LEFT JOIN categories ON wnews.category = categories.id
ORDER BY wnews.added DESC") or die(mysql_error());
while ($arr = mysql_fetch_assoc($query))
{
printNews($arr["torrentid"], $arr["title"], $arr["img"], $arr["added"], $arr["cat_name"], $arr["genre"], $arr["uploader"], $arr["description"]);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我喜欢在创建复杂的 sql 语句(带有内部联接等)时使用 MySQL 查询浏览器。
您应该可以在这里下载:
http://dev.mysql.com/downloads/
香农
I like using the MySQL Query Browser when creating complex sql statements (with inner joins and such).
You should be able to download it here:
http://dev.mysql.com/downloads/
Shannon