mysql - How to get row position from sql table in Php -
i need 'position (row)' sql table using php script. tested sql statement , worked fine. output correct.
my sql statement:
set @rownum = 0; select position, name,cash (select name, cash, @rownum := @rownum + 1 position 'my_db'.'cash' order cash desc ) t name = 'user44';
output:
"position":44,"name":"user44","cash":"5600"
but if put in php:
$query="set @rownum = 0; select position, name,cash (select name, cash, @rownum := @rownum + 1 position 'my_db'.'cash' order cash desc ) t name = 'user44';";
it shows me errant query
.
i tried this:
$query="select position, name,cash (select name, cash, @rownum := @rownum + 1 position 'my_db'.'cash' order cash desc ) t name = 'user44';";
output:
"position":null,"name":"user44","cash":"5600"
thanks volkerk, solved problem with:
$query = "set @rownum =0;"; $result = mysql_query($query,$link) or die('errant query: '.$query); $query="select position, name,cash (select name, cash, @rownum := @rownum + 1 position 'my_db'.'hscash' order cash desc ) t name = 'user44'"; $result = mysql_query($query,$link) or die('errant query: '.$query);
i understand better now.
the trick not position, it's number of people have better score user.
sql:
// how cash user make select cash mydb.cash name = :name; // how many people did better + 1 select count(*)+1 mydb.cash cash > :cash
the contatenated query more complicated, didn't need order whole table, , looks like
select position, name, cash mydb.cash join ( select count(*)+1 position mydb.cash cash > ( select cash mydb.cash name = :name ) ) name = :name
or, limit join filtering before joining, :
select position, name, cash ( select name, cash mydb.cash name = :name ) join ( select count(*)+1 position mydb.cash cash > ( select cash mydb.cash name = :name ) )
Comments
Post a Comment