首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > Mysql >

表字段值与另一表字段名相同有关问题

2012-04-23 
表字段值与另一表字段名相同问题。现有表A:A1A2A312工资23分红31奖金表B:工资奖金加班工资分红....(还有很

表字段值与另一表字段名相同问题。
现有表A:  
  A1 A2 A3  
  1 2 工资  
  2 3 分红  
  3 1 奖金  

表B:
  工资 奖金 加班工资 分红 ....(还有很多其他字段)
  1 3 1 2
  2 2 3 2
  3 1 3 3

怎样根据表A的A3字段值将表B里对应字段名(表A的A3字段值为表B的全部字段名的一部分)的内容取出来?求高手指点一二!感激不尽!!



[解决办法]
猜 lz 要的是这个?

SQL code
mysql> select * from config;+----+----------+| id | name     |+----+----------+|  1 | salary   ||  2 | bonus    ||  3 | overtime |+----+----------+3 rows in set (0.00 sec)mysql> select * from data;+--------+-------+----------+| salary | bonus | overtime |+--------+-------+----------+|      1 |     2 |        4 ||      3 |     3 |        3 |+--------+-------+----------+2 rows in set (0.00 sec)mysql> select @sql := concat("select ", group_concat(name), " from data;")    -> from config    -> where id in (1, 2);+--------------------------+| @sql := concat("select ", group_concat(name), " from data;") |+--------------------------+| select salary,bonus from data;                               |+--------------------------+1 row in set (0.00 sec)mysql>mysql> prepare sp from @sql;Query OK, 0 rows affected (0.00 sec)Statement preparedmysql> execute sp;+--------+-------+| salary | bonus |+--------+-------+|      1 |     2 ||      3 |     3 |+--------+-------+2 rows in set (0.00 sec)mysql> drop prepare sp;Query OK, 0 rows affected (0.00 sec)
[解决办法]
http://blog.csdn.net/acmain_chm/article/details/4283943
MySQL交叉表
在某些数据库中有交叉表,但在MySQL中却没有这个功能,但网上看到有不少朋友想找出一个解决方法,特发贴集思广义。http://topic.csdn.net/u/20090530/23/0b782674-4b0b-4cf5-bc1a-e8914aaee5ab.html?96198现整理解法如下:数据样本: create table tx( id int primary key, c1 c...

热点排行