Page 1 of 1

SQL SELECT

Posted: 2011 May 30 14:28
by addinol
Sveiki, taigi reikia gauti kills, deaths ir skill reikšmę, ir gauti žaidėjo vietą, jei pagal skill mažėjančia tvarka.

Mano table atrodo taip:
id, ip, name, kills, deaths, headshots, skill.

Turiu tokį kodą kuris gauna, kills, deaths, headshots ir gauna žaidėjo vietą, rikiuojant pagal (kills + headshots) / deaths, viskas veikia.

Code: Select all

public display_rank(id) {    new steam_id[32]    get_user_authid(id, steam_id, 31)     format(MySQL_Query, 511, "SELECT rank, (SELECT COUNT(*) FROM stats) AS total, kills, deaths, headshots FROM (SELECT @rank:=@rank+1 AS rank, steam, kills, deaths, headshots FROM stats, (SELECT @rank:=0) NONE ORDER BY ((kills + headshots) / deaths) DESC) AS result WHERE steam_id='%s'", steam_id)    new Handle:Query = SQL_PrepareQuery(MySQL_Connection, MySQL_Query)     if(SQL_Execute(Query) && SQL_MoreResults(Query)) {        new rank = SQL_ReadResult(Query, 0)        new total = SQL_ReadResult(Query, 1)        new kills = SQL_ReadResult(Query, 2)        new deaths = SQL_ReadResult(Query, 3)        new headshots = SQL_ReadResult(Query, 4)        client_print(id, print_chat, "You are rank %i of %i, with %i kills, %i headshots and %i deaths", rank, total, kills, headshots, deaths)    }} 
 
Bet bandžiau pats padaryti, bet kažkodėl nerodo išvis tada.
 
Štai kaip aš bandžiau padaryti:

Code: Select all

public display_rank(id) {    new steam[32]    get_user_authid(id, steam, charsmax(steam))     formatex(MySQL_Query, 511, "SELECT rank, (SELECT COUNT(*) FROM stats) AS total, kills, deaths, headshots, skill FROM (SELECT @rank:=@rank+1 AS rank, steam, kills, deaths, headshots, skill FROM stats, (SELECT @rank:=0) NONE ORDER BY (skill DESC) AS result WHERE steam='%s'", steam)    new Handle:Query = SQL_PrepareQuery(MySQL_Connection, MySQL_Query)     if(SQL_Execute(Query) && SQL_MoreResults(Query)) {        new rank = SQL_ReadResult(Query, 0)        new total = SQL_ReadResult(Query, 1)        new kills = SQL_ReadResult(Query, 2)        new deaths = SQL_ReadResult(Query, 3)        new headshots = SQL_ReadResult(Query, 4)        new skill = SQL_ReadResult(Query, 5)                if(kills + headshots > 5 && deaths > 5)            client_print(id, print_chat, "Your rank is %i of %i with %i skill", rank, total, skill )        else            client_print(id, print_chat, "You can not be ranked, you do not have any kills or deaths")    }}

Re: SQL SELECT

Posted: 2011 May 30 14:51
by aaarnas
Patikrink kokią klaidą grąžina mysql:

Code: Select all

new klaida[200]SQL_QueryError(Query, klaida, charsmax(klaida)log_amx(klaida)

Re: SQL SELECT

Posted: 2011 May 30 15:10
by addinol
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near `DESC> AS result WHERE steam='.....' at line 1

Re: SQL SELECT

Posted: 2011 May 30 15:39
by aaarnas
Iš šios klaidos nelabai galiu pasakyti problemos priežasties, nes kiek sudėtinga man ši užklausa. Pamėgink dar šitaip:

log_amx(MySQL_Query)

Po to tą suformatuotą query nukopijuok phpmyadmin sql langelyje ir paleisk. Ten daugiau info parodys.

Re: SQL SELECT

Posted: 2011 May 30 17:10
by addinol
amx log:

SELECT rank, (SELECT COUNT(x) FROM stats) AS total, kills, deaths, headshots, skill FROM (SELECT @rank:=@rank+1 AS rank, steam, kills, deaths, headshots, skill FROM stats, (SELECT @rank:=@) NONE ORDER BY (skill DESC) AS result WHERE steam='....'

o phpmyadmin'e toks pat erroras koks ir su log_amx(klaida)...

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DESC) AS result WHERE steam="...'' at line 1

Re: SQL SELECT

Posted: 2011 May 30 18:24
by aaarnas
Fail.
SELECT rank, (SELECT COUNT(x) FROM stats) AS total, kills, deaths, headshots, skill FROM (SELECT @rank:=@rank+1 AS rank, steam, kills, deaths, headshots, skill FROM stats, (SELECT @rank:=@) NONE ORDER BY (skill DESC) AS result WHERE steam='....'
Nuimk skliaustą.

Arba uždėk po DESC.

Re: SQL SELECT

Posted: 2011 May 30 19:13
by addinol
Jo dėkui, žioplos klaidos.. ;D

dar nesuprantu kodėl mano skill būna 0, nors aš turiu 25 kills 18 headshots ir 6 deaths, ir jis turetų būti sugeneruojamas kai nušauna žmogu (ps. Kills headshots deaths įrašo gerai.)

PasteBin