简体   繁体   English

如何在MySQL中使用SELECT语句从一个表中获取数据,并从另一表中覆盖数据?

[英]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 . 我有两个表: movieitemsmovieitems_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.

相关问题 MYSQL仅在该表的列值与另一表的列值匹配的情况下,才如何从该表中选择数据? - MYSQL How do I select data from one table only where column values from that table match the column values of another table? MySQL我可以从一个表还是另一个表中选择数据? - MySQL Can I Select Data from One table OR another Table? 如何在MySQL中将数据从一个表移到另一个表? - How do I move data from one table into another in MySQL? 如何在mysql中将数据从一个表插入到另一个表? - How do I insert data from one table to another in mysql? 如何从一个表中选择被MySQL中的另一个表过滤的行? - How do I select rows from one table filtered by another table in MySQL? MySQL语句从一个表读取数据并检查另一张表 - MySQL statement to read data from one table with checks on another table MySql表类型,如何将列数据从一个表自动绘制到另一个表? - MySql table type, how do I draw column data from one table to another automatically? MYSQL:如何根据另一个表中的数据选择列? - MYSQL: How do select column depending on data from another table? 如何将数据从一个表复制到另一表? - How do i copy data from one table to another table? 根据另一个表中的select语句从一个表中选择一个mysql行 - Selecting a mysql row from one table based on select statement in another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM