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:deletequery builders - Multiple conditions — chain multiple
:wherecalls to apply additional filters
Parameters
| Type | Name | Description |
|---|---|---|
string | column | Name of the column to filter on |
string | operator | Comparison operator to apply Refer Operators section |
string | number | value | Value to compare the column against |
Returns
| Type | Name | Description |
|---|---|---|
db_query | bool | query | Query builder instance on successful execution, or false on failure |
Operators
| Operator | Description |
|---|---|
"=" | Equal to |
"!=" | Not equal to |
">" | Greater than |
"<" | Less than |
">=" | Greater than or equal to |
"<=" | Less than or equal to |
Examples
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()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()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()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()