【发布时间】:2014-07-04 15:27:35
【问题描述】:
现在我正在使用 UNION 从多个表中获取记录,这很好。但我想包括特定帖子的作者。
$run = mysql_query("
SELECT article_id AS id, title, smalldesc, hits, coverpic AS picture, timestamp, member_id AS mid, type FROM articles
UNION
SELECT video_id AS id, titel, smalldesc, hits, ytid AS picture, timestamp, member_id AS mid, type FROM videos
UNION
SELECT picture_id AS id, title, smalldesc, hits, coverpic AS picture, timestamp, member_id AS mid, type FROM pictures ORDER BY timestamp DESC LIMIT ".$postnumbers." OFFSET ".$offset."
") or die(mysql_error());
我希望以下选择成为上述代码的一部分:
SELECT member_id, picture, fname, lname FROM members WHERE member_id='mid'
我想从每个帖子中获取成员,这样我就可以打印出谁发了帖子。
【问题讨论】:
-
Please, don't use
mysql_*functions in new code。它们不再维护and are officially deprecated。看到red box?改为了解 prepared statements,并使用 PDO 或 MySQLi - this article 将帮助您决定哪个。 -
你好 Kermit,我在大多数事情上都使用 MySQLi,但我必须先学习一些东西。我会将 mysql 代码“转换”为 mysqli。我有一个现在在线的网站,我需要这个功能,这就是为什么我使用“旧”mysql :) 但是谢谢。我会检查这些链接。
-
您似乎在使用
titel的第二个SELECT 中有错字。在这种情况下,准备好的语句毫无意义。也帮自己一个忙,在切换时使用PDO,而不是mysqli权宜之计API,因为感知到的名称相似。 - 顺便说一句,您还可以将所有三个 UNIONed 查询包装在一个子选择中,并将LEFT JOIN与您的成员表一起包装。也许更具可读性。 -
@mario 但只有在说明的情况下,索引也将用于子选择...