mysql动态添加表名 列名,MySQL存储功能-动态/可变表和列名

I'm learning stored procedures/functions/triggers in MySQL this morning and I'm having some problems trying to use variable table and column names in queries.

DELIMITER |

USE R2R |

DROP FUNCTION IF EXISTS getCategoryName |

CREATE FUNCTION getCategoryName(LMID INT, levelNum INT)

RETURNS VARCHAR(255)

BEGIN

DECLARE levelName VARCHAR(255) DEFAULT makeLevelName(levelNum);

DECLARE levelID INT;

DECLARE levelNameID VARCHAR(255) DEFAULT CONCAT(levelName, 'ID');

DECLARE ret VARCHAR(255);

SELECT @levelNameID INTO levelID FROM LevelMaster WHERE id=LMID;

SELECT description INTO ret FROM @levelName WHERE id=levelID;

RETURN ret;

END;

|

DROP FUNCTION IF EXISTS makeLevelName |

CREATE FUNCTION makeLevelName(levelNum INT)

RETURNS VARCHAR(255)

BEGIN

DECLARE word VARCHAR(255);

DECLARE ret VARCHAR(255);

IF levelNum=2 THEN SET word='Two';

ELSEIF levelNum=3 THEN SET word='Three';

ELSEIF levelNum=4 THEN SET word='Four';

ELSEIF levelNum=5 THEN SET word='Five';

END IF;

SET ret=CONCAT('Level', word);

RETURN ret;

END;

|

SELECT getCategoryName(347, 2) |

It's the first function (getCategoryName) that's causing me the problems, I need the two variables marked with @ to be the table/column names - these two lines:

SELECT @levelNameID INTO levelID FROM LevelMaster WHERE id=LMID;

SELECT description INTO ret FROM @levelName WHERE id=levelID;

I want to keep this function as a function rather than a procedure if possible, but would accept answers for a procedure if it's the only way.

Thanks for you help,

Richard

解决方案

Use User/Global Vars for this along with PREPARE & EXECUTE:

SET @columnName='myColumn';

SET @tableName='myTable';

SET @whatEver='requiredValue';

SET @query=CONCAT('SELECT ', @columnName, ' FROM ', @tableName, ' WHERE Column=', @whatEver);

PREPARE QUERY FROM @QUERY;

EXECUTE QUERY;

Haven't tested this EXACT code but something along these lines will work. Also has to be inside a Procedure, cannot be used with a function or trigger, if anyone has a soloution for that then please post.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要获取MySQL数据库中名和名,可以使用以下Java代码: ```java import java.sql.*; public class MySQLMetadata { static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/your_database_name"; static final String USER = "your_username"; static final String PASS = "your_password"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try { Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(DB_URL, USER, PASS); DatabaseMetaData meta = conn.getMetaData(); System.out.println("List of Tables: "); ResultSet rs = meta.getTables(null, null, null, new String[] {"TABLE"}); while (rs.next()) { System.out.println(rs.getString("TABLE_NAME")); } System.out.println("List of Columns: "); stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT * FROM your_table_name"); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); for (int i = 1; i <= columnCount; i++) { System.out.println(rsmd.getColumnName(i)); } rs.close(); stmt.close(); conn.close(); } catch (SQLException se) { se.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (stmt != null) stmt.close(); } catch (SQLException se2) { } try { if (conn != null) conn.close(); } catch (SQLException se) { se.printStackTrace(); } } } } ``` 其中,`getTables()` 方法可以获取数据库中的所有名,而 `getColumnName()` 方法可以获取指定名中的所有名。在代码中,需要修改的地方包括: - `DB_URL`:将 `your_database_name` 替换为实际的数据库名。 - `USER` 和 `PASS`:将 `your_username` 和 `your_password` 替换为实际的数据库用户名和密码。 - `rs = stmt.executeQuery("SELECT * FROM your_table_name")`:将 `your_table_name` 替换为实际的名。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值