self:select

Server

Specifies the columns to retrieve when executing a fetch query


Syntax

local query = self:select(
    ...
)

This function only prepares the query and does not execute it. Chain db_query:fetch at the end to retrieve the results.

  • Chaining — compatible with :where to apply filters to the selection
  • Multiple selections — chain multiple :select calls to append additional columns to the selection
  • Without selections — all columns are returned, equivalent to SELECT *
  • Multiple conditions — chain multiple :where calls to apply additional filters

Parameters

TypeNameDescription
string...One or more column names to retrieve from the table
Omit to return all columns

Returns

TypeNameDescription
db_query | boolqueryQuery builder instance on successful execution, or false on failure

Examples

Fetch specific columns from the 'players' table
local db = database.create("127.0.0.1", "root", "", "vital_sandbox")

thread.create(function(self)
    local ok, rows = self:await(db:table("players")
        :select("id", "name", "score")
        :fetch())

    if ok then
        for i = 1, #rows do
            engine.print("info", rows[i].id, rows[i].name, rows[i].score)
        end
    end
end):resume()
Fetch all columns from the 'players' table
local db = database.create("127.0.0.1", "root", "", "vital_sandbox")

thread.create(function(self)
    local ok, rows = self:await(db:table("players")
        :fetch())

    if ok then
        for i = 1, #rows do
            engine.print("info", rows[i].id, rows[i].name, rows[i].score)
        end
    end
end):resume()

On this page