永发信息网

mysql中,一个信息表有很多字段,是把他拆分成几个表呢,还是就在一个表中记录,哪种更好?

答案:2  悬赏:30  手机版
解决时间 2021-01-26 16:55
比方说,一个商家的信息表,有一些字段是属于联系方式类的,如qq、电话、地址......,是把这些联系方式写在另一个表里,还是就在原信息表中?
最佳答案
看你需要对表作什么操作
一般情况下,在一张表中字段比较多是没任何问题的
这样通过一张表结构就可以查询到所有的商家数据
写sql的增删查改的语句也比较简单,性能比较高
如果是分开几个表,就需要用到外码,左右联接查询之类的,这样性能反而比较低
所以,还是建议把这些数据都写在一张表里
如果,在查询的时候不需要用到联系方式这些数据
只要在select语句中不写那个字段就行了
也不会说看起来很冗杂
全部回答
争议觉得你还是应该用php去写。非要用mysql本身的话给个参考: mysql> select * from a;+------+--------+------+----+| id | name | num1 | bb |+------+--------+------+----+| 1 | a, | 2 | 3 || 2 | b,c, | 2 | 4 || 3 | d,e,f, | 2 | 5 |+------+--------+------+----+3 rows in set (0.00 sec)mysql> delimiter //mysql> create procedure `strsplit1`(in str3 varchar(2)) -> begin -> declare i int(10); -> declare j int(10); -> declare k int(10); -> declare str1 varchar(100); -> declare str4 varchar(1000); -> declare str5 varchar(100); -> -> select count(*) into i from a; -> if i!=0 then -> drop table if exists tmp_table1; -> create temporary table tmp_table1 like a; -> end if; -> -> set j=0; -> while j select id,name into str1,str4 from a limit j,1; -> select instr(str4,str3) into k from dual; -> if k=0 then -> insert into tmp_table1(id,name) values(str1,str4); -> end if; -> while k!=0 do -> select substring_index(str4,str3,1) into str5 from dual; -> insert into tmp_table1(id,name) values(str1,str5); -> select mid(str4,k+1) into str4 from dual; -> select instr(str4,str3) into k from dual; -> end while; -> set j=j+1; -> end while; -> -> select * from tmp_table1; -> end -> //query ok, 0 rows affected (0.00 sec)mysql> delimiter ;最后结果:mysql> call strsplit1(',');+------+------+------+----+| id | name | num1 | bb |+------+------+------+----+| 1 | a | null | 1 || 2 | b | null | 2 || 2 | c | null | 3 || 3 | d | null | 4 || 3 | e | null | 5 || 3 | f | null | 6 |+------+------+------+----+6 rows in set (0.25 sec)query ok, 0 rows affected (0.30 sec) 希望能解决您的问题。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯