我有两张表,一张用于评论(id、title、cover_art、text、rating、updated),另一张用于艺术家(id、artist_name)。
我在评论表中有一个艺术家 ID 列。
直到今天早些时候,我只是访问了一个连接两个表的表,但事实证明这会导致未来出现问题。
下面是一个 JOIN 代码需要去哪里的例子:
mysql_select_db($database_em, $em);
$query_getDisplay = "SELECT news.title, news.cover_art, news.blog_entry, news.rating, DATE_FORMAT(news.updated, '%M %e, %Y') AS formatted FROM news ORDER BY news.updated DESC LIMIT 1";
$getDisplay = mysql_query($query_getDisplay, $em) or die(mysql_error());
$row_getDisplay = mysql_fetch_assoc($getDisplay);
$totalRows_getDisplay = mysql_num_rows($getDisplay);
这是我想加入的 JOIN:
artists.artists_name
FROM news JOIN artists ON artists.id = news.artistid
但是无论我把它放在我认为它应该去的地方(在 news.rating 和 DATE_FORMAT 之间用逗号跟在它后面),分页符都会出现这个错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(news.updated, '%M %e, %Y') AS formatted FROM news ORDER BY news.updated DESC LI' at line 2
这是整个代码减去我在标题中的 sql 注入开关代码。我已经尝试在 news.rating 的每个实例之间添加 JOIN 代码,以及 DATE_FORMAT 和各种其他想法,以及查看 stackoverflow 的知识库(顺便说一句,这是基于我在 David Powers 教程中使用的代码,所以其中大部分甚至可能多余)
mysql_select_db($database_em, $em);
$query_getArchives = "SELECT DISTINCT DATE_FORMAT(news.updated, '%M %Y') AS archive, DATE_FORMAT(news.updated, '%Y-%m') AS link FROM news ORDER BY news.updated DESC";
$getArchives = mysql_query($query_getArchives, $em) or die(mysql_error());
$row_getArchives = mysql_fetch_assoc($getArchives);
$totalRows_getArchives = mysql_num_rows($getArchives);
mysql_select_db($database_em, $em);
$query_getRecent = "SELECT news.post_id, news.title FROM news ORDER BY news.updated DESC LIMIT 20";
$getRecent = mysql_query($query_getRecent, $em) or die(mysql_error());
$row_getRecent = mysql_fetch_assoc($getRecent);
$totalRows_getRecent = mysql_num_rows($getRecent);
mysql_select_db($database_em, $em);
$query_getDisplay = "SELECT news.title, news.cover_art, news.blog_entry, news.rating, artists.artists_name
FROM news JOIN artists ON artists.id = news.artistid, DATE_FORMAT(news.updated, '%M %e, %Y') AS formatted FROM news ORDER BY news.updated DESC LIMIT 1";
$getDisplay = mysql_query($query_getDisplay, $em) or die(mysql_error());
$row_getDisplay = mysql_fetch_assoc($getDisplay);
$totalRows_getDisplay = mysql_num_rows($getDisplay);
$var1_getDisplay2 = "-1";
if (isset($_GET['archive'])) {
$var1_getDisplay2 = $_GET['archive'];
$query_getDisplay = sprintf("SELECT news.blog_entry, news.cover_art, news.rating, DATE_FORMAT(news.updated, '%%M %%e, %%Y') AS formatted FROM news WHERE DATE_FORMAT(news.updated, '%%Y-%%m') = %s ORDER BY news.updated DESC", GetSQLValueString($var1_getDisplay2, "text"));
} elseif (isset($_GET['post_id'])) {
$var2_getDisplay3 = $_GET['post_id'];
$query_getDisplay = sprintf("SELECT news.title, news.cover_art, news.blog_entry, news.rating, DATE_FORMAT(news.updated, '%%M %%e, %%Y') AS formatted FROM news WHERE news.post_id = %s", GetSQLValueString($var2_getDisplay3, "int"));
} else {
$query_getDisplay = "SELECT news.title, news.cover_art, news.blog_entry, news.rating, DATE_FORMAT(news.updated, '%M %e, %Y') AS formatted FROM news ORDER BY news.updated DESC LIMIT 3";
}
$getDisplay = mysql_query($query_getDisplay, $em) or die(mysql_error());
$row_getDisplay = mysql_fetch_assoc($getDisplay);
$totalRows_getDisplay = mysql_num_rows($getDisplay);
?>
顺便说一句,这是实际页面中的代码:
<!-- start center column include -->
<div class="center_column">
<div class="center_column_title">
<?php echo date('H:i:s'); ?> GMT
</div>
<div id="blog_posts">
<?php do { ?>
<h2><?php echo $row_getDisplay['title']; ?></h2>
<h3><?php echo $row_getDisplay['artists_name']; ?></h3>
<p class="cover_art"><img src="<?php echo $row_getDisplay['cover_art']; ?>" /></p>
<p class="updated">Updated <?php echo $row_getDisplay['formatted']; ?></p>
<p class="blog_entry"><?php echo nl2br($row_getDisplay['blog_entry']); ?></p>
<p class="blog_rating">Rating - <?php echo $row_getDisplay['rating']; ?> (out of a possible 1,000,000)</p>
<?php } while ($row_getDisplay = mysql_fetch_assoc($getDisplay)); ?>
</div>
<p> </p>
</div>
<!-- end center column include -->