想请问下MySQL能不能把select关联表的结果存储到主表中?

WBOY
Release: 2016-06-06 20:13:10
Original
762 people have browsed it

是由这个如何在MySQL中存储数组的问题 产生的问题:

问题中解决存储数组的办法是做三个表,分别是uid,uid_hobby,hobby表,要查某个uid的hobby的话只能再执行select uid from uid_hobby where uid = XXX,这样每次都要执行一边挺麻烦的。

想请问下MySQL能不能把对关联表select的结果存储到主表的column中?比如uid表中有一列hobby列,这列里面存放的内容是“select uid from uid_hobby where uid = 当前id”这句话的结果?或者有什么方式可以让这个查询过程自动化一点?在创建表的时候就把这种对应关系定好,这样在查询主表的时候就能一次性查询出来,不用再去查询一遍。

就是说在select hobby from uid where uid = XXX的时候就能直接查询到did对应的hobby而不是还要再执行对关联表的查询。这样对一张表的查询只用查询一张表就完成了。

回复内容:

是由这个如何在MySQL中存储数组的问题 产生的问题:

问题中解决存储数组的办法是做三个表,分别是uid,uid_hobby,hobby表,要查某个uid的hobby的话只能再执行select uid from uid_hobby where uid = XXX,这样每次都要执行一边挺麻烦的。

想请问下MySQL能不能把对关联表select的结果存储到主表的column中?比如uid表中有一列hobby列,这列里面存放的内容是“select uid from uid_hobby where uid = 当前id”这句话的结果?或者有什么方式可以让这个查询过程自动化一点?在创建表的时候就把这种对应关系定好,这样在查询主表的时候就能一次性查询出来,不用再去查询一遍。

就是说在select hobby from uid where uid = XXX的时候就能直接查询到did对应的hobby而不是还要再执行对关联表的查询。这样对一张表的查询只用查询一张表就完成了。

select uid.* from (select uid from uid_hobby where hobby like '%游泳%') hobby_tb, uid where uid.id = hobby_tb.uid
Copy after login

不知道这样理解的对吗?如果是这样的话,你在调用的时候甚至不用再主表创建一个这样的字段了

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!