self:where

Server

Appends a filter condition to the query


Syntax

local query = self:where(
    column,
    operator,
    value
)

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

  • Chaining — compatible with :select, :update, and :delete query builders
  • Multiple conditions — chain multiple :where calls to apply additional filters

Parameters

TypeNameDescription
stringcolumnName of the column to filter on
stringoperatorComparison operator to apply
Refer Operators section
string | numbervalueValue to compare the column against

Returns

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

Operators

OperatorDescription
"="Equal to
"!="Not equal to
">"Greater than
"<"Less than
">="Greater than or equal to
"<="Less than or equal to

Examples

Filter rows with a single condition
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")
        :where("score", ">", 100)
        :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()
Chain multiple conditions to filter rows
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")
        :where("name", "=", "aviril")
        :where("score", ">=", 500)
        :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()
Update rows with a single condition
local db = database.create("127.0.0.1", "root", "", "vital_sandbox")

thread.create(function(self)
    local ok, err = self:await(db:table("players")
        :update({ score = 500 })
        :where("name", "=", "aviril")
        :execute())

    if not ok then
        engine.print("error", err)
    end
end):resume()
Chain multiple conditions to update rows
local db = database.create("127.0.0.1", "root", "", "vital_sandbox")

thread.create(function(self)
    local ok, err = self:await(db:table("players")
        :update({ score = 0 })
        :where("status", "=", "active")
        :where("score", "<", 50)
        :execute())

    if not ok then
        engine.print("error", err)
    end
end):resume()

On this page