开发者

Using mySQL variables

开发者 https://www.devze.com 2023-02-06 11:47 出处:网络
Is it possible in a sequence of SQL statements to get the value开发者_如何学Goof a field and use that to name a table in another statement? I\'m not sure if that\'s clear, so here\'s an psudo-example

Is it possible in a sequence of SQL statements to get the value 开发者_如何学Goof a field and use that to name a table in another statement? I'm not sure if that's clear, so here's an psudo-example of what I'm trying to do:

// dataType is equal to "ratings"
@var = select dataType from theTable where anID = 5;

// needs to run as "from ratings-table"
select field1,field2 from @var-table where anID = 5;

I've been reading http://dev.mysql.com/doc/refman/5.0/en/user-variables.html but either I don't properly understand this, or its not the solution I'm looking for.


Yes, you can do this using prepared statements:

SET @TableName := 'ratings';
SET @CreateQuery := CONCAT('SELECT `field1`, `field2` FROM `', @TableName, '-table` WHERE `anID` = 5');
PREPARE statementCreate FROM @CreateQuery;
EXECUTE statementCreate;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号