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

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

现有表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 prepared

mysql> 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...
表字段值与另一表字段名相同有关问题

相关文章:

你感兴趣的文章:

标签云: