动态排序MYSQL存储过程/函数

时间:2011-01-18 19:58:56

标签: mysql

我有一个名为spGetVideosByLocation的MYSQL函数。目前我称之为:

CALL spGetVideosByLocation(location);

这基本上调用了类似这样的查询:

SELECT * FROM Videos WHERE LocationId = location;

我的问题是,当我调用函数时,有没有办法定义ORDER BY? 我如何得到这个:

CALL spGetVideosByLocation(location, "tableName", DESC);

要查询:

SELECT * FROM Videos WHERE LocationId = location ORDER BY tableName DESC;

1 个答案:

答案 0 :(得分:0)

您使用的是哪种编程语言?假设您使用的是php,我将使用以下代码:

function spGetVideosByLocation($locationid, $order, $orderby)
{
    $sql = "SELECT * FROM Videos WHERE LocationId = '$locationid' ORDER BY $order $orderby";
    $result = mysql_query($sql) or die(mysql_error());
    //do whatever you need to to get results
}