[英]How do I take data from one table, and overwrite it from another table, with a SELECT statement in MySQL?
Ok, let me try to explain this question... 好吧,让我尝试解释这个问题...
I have two tables: movieitems
and movieitems_custom
. 我有两个表: movieitems
和movieitems_custom
。 They both have identical columns of barcode , title , runtime , and username . 它们都有相同的条形码 , 标题 , 运行时和用户名列 。
I want a query or stored procure that will pull from movieitems
(to get a movie's details owned by a user) and then pull from and overwrite the return data from movieitems_custom
(to get a user's custom data entry). 我想要一个查询或存储过程,该查询或存储过程将从movieitems
(以获取用户拥有的电影的详细信息),然后从movieitems_custom
提取并覆盖返回数据(以获取用户的自定义数据条目)。
Note that movieitems_custom
doesn't always have a matching entry from movieitems
, only sometimes. 请注意, movieitems_custom
并非总是有来自movieitems
的匹配条目,只是有时是这样。
Here is an example: 这是一个例子:
movieitems
for a user contains these two items for user 'joe'
that he owns: 用户的movieitems
包含他拥有的用户'joe'
以下两项:
1234 - Batman Begins - 120 minutes 1234-蝙蝠侠开始-120分钟
1235 - 12 Monkeys - 97 minutes 1235-12猴子-97分钟
and in movieitems_custom
we could have one entry like this for 'joe'
that he customized: 在movieitems_custom
我们可以为他定制的'joe'
有一个这样的条目:
1234 - Batman 1 aka Batman Begins - 120 minutes 1234-蝙蝠侠1又名蝙蝠侠开始-120分钟
What I want my statement/procedure to return is this: 我想要我的陈述/程序返回的是:
1234 - Batman 1 aka Batman Begins - 120 minutes 1234-蝙蝠侠1又名蝙蝠侠开始-120分钟
1235 - 12 Monkeys - 97 minutes 1235-12猴子-97分钟
Is some type of JOIN
statement enough to make this happen, or am I looking at something more complex? 是某种类型的JOIN
语句足以实现此目的,还是我正在寻找更复杂的东西?
SELECT
I.barcode,
IFNULL(C.title, I.title) AS title,
I.runtime,
I.username
FROM
movieitems I
LEFT JOIN movieitems_custom C
ON I.barcode = C.barcode AND I.username = C.username
you need to use COALESCE which you can read about here 您需要使用COALESCE,您可以在此处阅读
http://msdn.microsoft.com/en-us/library/ms190349.aspx http://msdn.microsoft.com/en-us/library/ms190349.aspx
basically it will let you specify where to get the values from if any are NULL. 基本上,如果有任何NULL,它将让您指定从何处获取值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.