Mysql - 试图将一个结果返回到联合中
我想做的是查询数据库以根据所选的网站语言获取我的网站的所有频道。所以数据库中有一个包含所有通道的表。第二个表根据内容分配到的语言保存所有频道内容。假设我想要一个英语和西班牙语频道,所以频道表中会有一条记录,然后在channel_local表中有2个条目,两个条目都具有与主频道表相同的频道ID,但一个条目带有频道的英语版本和第二个频道的西班牙语版本。我如何查询主频道表并根据为站点选择的当前语言从channel_local表获取该频道的语言版本,但如果没有找到结果,则在一个查询中默认为站点的默认语言?我以为工会会起作用,但它有英语和西班牙语版本。这是查询:
SELECT * FROM (SELECT
channel_local.channelid,
channel_local.channelname,
channel.channelorder
FROM channel
INNER JOIN channel_local ON
channel.id = channel_local.channelid
WHERE channel.active = 1
AND channel_local.languageid = {currentlanguageid}
UNION
SELECT
channel_local.channelid,
channel_local.channelname,
channel.channelorder
FROM channel
INNER JOIN channel_local ON
channel.id = channel_local.channelid
WHERE channel.active = 1
AND channel_local.languageid = {defaultlanguageid})
AS channels
结果是:
- 频道 1 英语版本
- 频道 1 西班牙语版本
- 频道 2 英语版本
我正在寻找的结果是
- 频道 1 英语版本
- 频道 2 英语版本
如果当前语言是西班牙语,那么我想要结果:
- 频道 1西班牙语版本
- Channel 2 英语版本(因为没有西班牙语版本 频道 2)
What Im trying to do is query the database to get all the channels for my site based on the site language selected. So there is a table in the db for all channels. There is a second table that holds all the channel content, based on the language that content is assigned to. So lets say I want one channel available in english and spanish, so there would be a single record in the channel table, then in the channel_local table there are 2 entries, both having the same channel id from the main channel table but one entry with the english version of the channel and the second the spanish version of the channel. How could I query the main channel table and based on the current language selected for the site get that language version of the channel from the channel_local table, but if no results found it defaults to the default language for the site all within one query? I thought a union would work but it gets both english and spanish version. Here is the query:
SELECT * FROM (SELECT
channel_local.channelid,
channel_local.channelname,
channel.channelorder
FROM channel
INNER JOIN channel_local ON
channel.id = channel_local.channelid
WHERE channel.active = 1
AND channel_local.languageid = {currentlanguageid}
UNION
SELECT
channel_local.channelid,
channel_local.channelname,
channel.channelorder
FROM channel
INNER JOIN channel_local ON
channel.id = channel_local.channelid
WHERE channel.active = 1
AND channel_local.languageid = {defaultlanguageid})
AS channels
The results are:
- Channel 1 English Version
- Channel 1 Spanish Version
- Channel 2 English Version
The results Im looking for is
- Channel 1 English Version
- Channel 2 English Version
If the current language is spanish then I would like the result:
- Channel 1 Spanish Version
- Channel 2 English Version (Because there is no spanish version for
channel 2)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于完整联合,您可以执行以下操作:
selectchannel_local.,channel. fromchannel_local,channelwherechannel.channelid=channel_local.channelidandchannel_local.channelidin(selectchannelidfromchannelwhere languageid='eng'andactive=1)
For a full union, you can do this:
select channel_local., channel. from channel_local, channel where channel.channelid=channel_local.channelid and channel_local.channelid in (select channelid from channel where languageid='eng' and active=1)
简单
地从
channel_local
中选择所有内容,其中该频道位于channel
表中,并且channel
处于活动状态,或者...
如果您处于活动状态字段位于
channel_local
表中,您可以放置此相关子查询可能会更快,而不是进行多个连接,这很快就会变得非常混乱;)
what about
simply select all from the
channel_local
where that channel is in thechannel
table and is thatchannel
is activeor...
if your active field is in the
channel_local
table you can putInstead of doing multiple joins, which get very confusing pretty quickly, this correlated subquery may be faster ;)