gpt4 book ai didi

php - 如何加入另一张 table ?

转载 作者:行者123 更新时间:2023-11-29 07:58:07 25 4
gpt4 key购买 nike

现在我正在使用 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());

我希望以下选择成为上面代码的一部分:

从成员中选择member_id、图片、fname、lname WHERE member_id='mid'

我想从每个帖子中获取成员,这样我就可以打印出帖子的发布者。

最佳答案

按照您的要求,您必须将 Members 表连接到 UNION 的三部分查询:

SELECT 
a.article_id AS id,
a.title,
a.smalldesc,
a.hits,
a.coverpic AS picture,
a.timestamp,
a.member_id AS mid,
a.type,
m.picture,
m.fname,
m.lname
FROM
articles a
INNER JOIN
members m
ON
a.member_id = m.member_id
UNION
SELECT
v.video_id,
v.titel,
v.smalldesc,
v.hits,
v.ytid AS picture,
v.timestamp,
v.member_id AS mid,
v.type
m2.picture,
m2.fname,
m2.lname
FROM
videos v
INNER JOIN
members m2
ON
v.member_id = m2.member_id
UNION
...

获取此信息。

关于php - 如何加入另一张 table ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24577415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com