![](/img/trans.png)
[英]MYSQL How do I select data from one table only where column values from that table match the column values of another table?
[英]How do I take data from one table, and overwrite it from another table, with a SELECT statement in MySQL?
好吧,让我尝试解释这个问题...
我有两个表: movieitems
和movieitems_custom
。 它们都有相同的条形码 , 标题 , 运行时和用户名列 。
我想要一个查询或存储过程,该查询或存储过程将从movieitems
(以获取用户拥有的电影的详细信息),然后从movieitems_custom
提取并覆盖返回数据(以获取用户的自定义数据条目)。
请注意, movieitems_custom
并非总是有来自movieitems
的匹配条目,只是有时是这样。
这是一个例子:
用户的movieitems
包含他拥有的用户'joe'
以下两项:
1234-蝙蝠侠开始-120分钟
1235-12猴子-97分钟
在movieitems_custom
我们可以为他定制的'joe'
有一个这样的条目:
1234-蝙蝠侠1又名蝙蝠侠开始-120分钟
我想要我的陈述/程序返回的是:
1234-蝙蝠侠1又名蝙蝠侠开始-120分钟
1235-12猴子-97分钟
是某种类型的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
您需要使用COALESCE,您可以在此处阅读
http://msdn.microsoft.com/en-us/library/ms190349.aspx
基本上,如果有任何NULL,它将让您指定从何处获取值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.