SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Help Archive

Thread Tools Display Modes
Prev Previous Post   Next Post Next
Old 05/10/2009, 10:47 AM   #1
Big Clucker
Join Date: May 2006
Posts: 79
Reputation: 1
Default SQL queries using GROUP BY

I'm not sure if this is a bug or some SQL thing, but using GROUP BY in SQL queries seems to break db_get_field_assoc.

pawn Code:
#include <a_samp>

public OnFilterScriptInit()
    new DB:db = db_open("test.sqlite");
    if(db) {
        db_free_result(db_query(db, "CREATE TABLE IF NOT EXISTS `Test` ( `string` TEXT NOT NULL DEFAULT '' , `value` INTEGER NOT NULL DEFAULT 0 ); DELETE FROM `Test`"));

        db_free_result(db_query(db, "INSERT INTO `Test` ( `string` , `value` ) VALUES ( 'abc' , 123 )"));
        db_free_result(db_query(db, "INSERT INTO `Test` ( `string` , `value` ) VALUES ( 'def' , 456 )"));
        db_free_result(db_query(db, "INSERT INTO `Test` ( `string` , `value` ) VALUES ( 'hij' , 789 )"));

        new DBResult:result = db_query(db, "SELECT `string` , `value` FROM `Test`"),
            rows = db_num_rows(result);
        for(new i; i < rows; i++) {
            new string[20],
            db_get_field_assoc(result, "value", string, sizeof(string));
            value = strval(string);
            db_get_field_assoc(result, "string", string, sizeof(string));
            printf("String: \"%s\", Value: %i", string, value);
    return 1;

Using this gives
String: "abc", Value: 123
String: "def", Value: 456
String: "hij", Value: 789
as expected, but when the SELECT query is changed to
pawn Code:
"SELECT `string` , `value` FROM `Test` GROUP BY `string`"

(or grouping by value), it gives
String: "", Value: 0
String: "", Value: 0
String: "", Value: 0
It does work properly when using the query
pawn Code:
"SELECT `string` AS `string` , `value` AS `value` FROM `Test` GROUP BY `string`"

but shouldn't that be the same?

Using db_field_name without the AS gives "`string`" and "`value`" instead of "string" and "value", maybe this is the problem.
Dabombber is offline  

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Little coding questions - For general minor queries 5 !damo!spiderman Scripting Help 4152 06/09/2020 02:02 AM
car for a group andrewp Help Archive 0 17/03/2010 12:32 AM
Group andrewp Help Archive 2 14/03/2010 06:12 AM
Counting with mysql queries actiwe Help Archive 2 26/02/2010 02:36 PM

All times are GMT. The time now is 12:31 PM.

Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.