Sql
in package
SQL Function
Tags
Table of Contents
- $sql : string
- SQL statement stored here
- $values : array<string|int, mixed>
- Array to store parameters for binding
- __construct() : mixed
- class constructer
- AVG() : static
- Calculate the average of the selected column
- BETWEEN() : static
- Generate a SQL BETWEEN ... AND ... clause
- CONCAT() : static
- Generate a SQL CONCAT or CONCAT_WS clause
- COUNT() : static
- Count the number of records for the selected column
- create() : mixed
- สร้าง Object Sql
- DATE() : static
- Extract date from a DATETIME column
- DATE_FORMAT() : static
- Format a date column for display
- DATEDIFF() : static
- Calculate the difference in days between two dates or between a date and NOW()
- DAY() : static
- Extract the day from a DATE or DATETIME column
- DISTINCT() : static
- Return distinct values of a column
- extractSort() : array<string|int, mixed>|string
- Extract sorting information and return as an array.
- fieldName() : string|int
- Wraps a column name with backticks (`) for SQL identifiers.
- FORMAT() : static
- Format a column for display
- getValues() : array<string|int, mixed>
- Retrieves or merges bind parameters ($values) used for prepared statements in SQL queries.
- GROUP_CONCAT() : static
- Create a GROUP_CONCAT SQL statement
- HOUR() : static
- Extract the hour from a DATETIME column
- IFNULL() : static
- Create an IFNULL SQL statement
- ISNOTNULL() : static
- Create an IS NOT NULL SQL statement
- ISNULL() : static
- Create an IS NULL SQL statement
- MAX() : static
- Find the maximum value of a column
- MIN() : static
- Find the minimum value of a column
- MINUTE() : static
- Extract minutes from a DATETIME column
- MONTH() : static
- Extract month from a DATE or DATETIME column
- NEXT() : static
- Generate SQL to find the next value in a sequence (MAX + 1)
- NOW() : static
- Returns the current date and time as a SQL function NOW().
- POSITION() : static
- Searches for a substring in a string and returns its position. If not found, returns 0; indexing starts from 1.
- quoteValue() : string|int
- Quotes and prepares a value for use in SQL queries, handling various data types and formats.
- RAND() : static
- Generates a random number.
- SECOND() : static
- Extracts the seconds from a DATETIME column.
- strValue() : static
- Creates a SQL string literal by wrapping the given value in single quotes ('').
- SUM() : static
- Calculates the sum of values in a selected column.
- text() : string
- Returns the SQL command as a string.
- TIMEDIFF() : static
- Calculates the time difference between two datetime columns or values.
- TIMESTAMPDIFF() : static
- Calculates the difference between two datetime columns or values in specified units.
- WHERE() : static
- Constructs a WHERE clause based on the provided conditions.
- YEAR() : static
- Extracts the year from a DATE or DATETIME column.
- buildWhere() : string
- Constructs SQL WHERE command based on given conditions.
Properties
$sql
SQL statement stored here
protected
string
$sql
$values
Array to store parameters for binding
protected
array<string|int, mixed>
$values
Methods
__construct()
class constructer
public
__construct([string $sql = null ]) : mixed
Parameters
- $sql : string = null
Return values
mixed —AVG()
Calculate the average of the selected column
public
static AVG(string $column_name[, string|null $alias = null ][, bool $distinct = false ]) : static
This function generates a SQL expression to calculate the average of a specified column.
Parameters
- $column_name : string
-
The name of the column to calculate the average for
- $alias : string|null = null
-
The alias for the resulting column, optional
- $distinct : bool = false
-
If true, calculates the average of distinct values only; default is false
Tags
Return values
static —BETWEEN()
Generate a SQL BETWEEN ... AND ... clause
public
static BETWEEN(string $column_name, string $min, string $max) : static
This function creates a SQL BETWEEN clause for a specified column and range.
Parameters
- $column_name : string
-
The name of the column for the BETWEEN clause
- $min : string
-
The minimum value for the range
- $max : string
-
The maximum value for the range
Tags
Return values
static —CONCAT()
Generate a SQL CONCAT or CONCAT_WS clause
public
static CONCAT(array<string|int, mixed> $fields[, string|null $alias = null ][, string|null $separator = null ]) : static
This function creates a SQL CONCAT or CONCAT_WS clause with optional alias and separator.
Parameters
- $fields : array<string|int, mixed>
-
List of fields to concatenate
- $alias : string|null = null
-
The alias for the resulting concatenation, optional
- $separator : string|null = null
-
Null (default) to use CONCAT, specify a separator to use CONCAT_WS
Tags
Return values
static —COUNT()
Count the number of records for the selected column
public
static COUNT([string $column_name = '*' ][, string|null $alias = null ][, bool $distinct = false ]) : static
This function generates a SQL COUNT expression for a specified column.
Parameters
- $column_name : string = '*'
-
The name of the column to count, defaults to '*'
- $alias : string|null = null
-
The alias for the resulting count, optional
- $distinct : bool = false
-
If true, counts only distinct values; default is false
Tags
Return values
static —create()
สร้าง Object Sql
public
static create(string $sql) : mixed
Parameters
- $sql : string
Return values
mixed —DATE()
Extract date from a DATETIME column
public
static DATE(string $column_name[, string|null $alias = null ]) : static
This function generates a SQL DATE expression to extract the date part from a DATETIME column.
Parameters
- $column_name : string
-
The name of the DATETIME column
- $alias : string|null = null
-
The alias for the resulting date, optional
Tags
Return values
static —DATE_FORMAT()
Format a date column for display
public
static DATE_FORMAT(string $column_name, string $format[, string|null $alias = null ]) : static
This function generates a SQL DATE_FORMAT expression to format a date column for display.
Parameters
- $column_name : string
-
The name of the date column
- $format : string
-
The format string for date formatting
- $alias : string|null = null
-
The alias for the resulting formatted date, optional
Tags
Return values
static —DATEDIFF()
Calculate the difference in days between two dates or between a date and NOW()
public
static DATEDIFF(string $column_name1, string $column_name2[, string $alias = null ]) : static
This function generates a SQL DATEDIFF expression to calculate the difference in days between two dates.
Parameters
- $column_name1 : string
-
The first date column or a specific date string
- $column_name2 : string
-
The second date column or a specific date string
- $alias : string = null
-
The alias for the resulting difference, optional
Tags
Return values
static —DAY()
Extract the day from a DATE or DATETIME column
public
static DAY(string $column_name[, string|null $alias = null ]) : static
This function generates a SQL DAY expression to extract the day from a DATE or DATETIME column.
Parameters
- $column_name : string
-
The name of the DATE or DATETIME column
- $alias : string|null = null
-
The alias for the resulting day, optional
Tags
Return values
static —DISTINCT()
Return distinct values of a column
public
static DISTINCT(string $column_name[, string|null $alias = null ]) : static
This function generates a SQL DISTINCT expression to return unique values of a column.
Parameters
- $column_name : string
-
The name of the column to retrieve distinct values from
- $alias : string|null = null
-
The alias for the resulting distinct values, optional
Tags
Return values
static —extractSort()
Extract sorting information and return as an array.
public
static extractSort(array<string|int, mixed> $columns, string $sort[, array<string|int, mixed> $default = [] ]) : array<string|int, mixed>|string
This function processes sorting parameters and returns an array of valid sorting instructions.
Parameters
- $columns : array<string|int, mixed>
-
List of columns that are valid for sorting.
- $sort : string
-
Sorting instructions in the format 'column_name direction'.
- $default : array<string|int, mixed> = []
-
Default sorting array if no valid sort instructions are provided.
Tags
Return values
array<string|int, mixed>|string —fieldName()
Wraps a column name with backticks (`) for SQL identifiers.
public
static fieldName(string|int $column_name) : string|int
Column names should consist of English letters, numbers, and underscores only. If the column name contains any other characters, returns it wrapped in single quotes ('').
Parameters
- $column_name : string|int
-
The column name or value to be formatted for SQL.
Tags
Return values
string|int —FORMAT()
Format a column for display
public
static FORMAT(string $column_name, string $format[, string|null $alias = null ]) : static
This function generates a SQL FORMAT expression to format a column for display.
Parameters
- $column_name : string
-
The name of the column to format
- $format : string
-
The format string for formatting
- $alias : string|null = null
-
The alias for the resulting formatted column, optional
Tags
Return values
static —getValues()
Retrieves or merges bind parameters ($values) used for prepared statements in SQL queries.
public
getValues([array<string|int, mixed> $values = [] ]) : array<string|int, mixed>
Parameters
- $values : array<string|int, mixed> = []
-
Optional. An array of bind parameters to merge or retrieve.
Return values
array<string|int, mixed> —GROUP_CONCAT()
Create a GROUP_CONCAT SQL statement
public
static GROUP_CONCAT(string $column_name[, string|null $alias = null ][, string $separator = ',' ][, bool $distinct = false ][, string|array<string|int, mixed> $order = null ]) : static
This function generates a SQL GROUP_CONCAT expression to concatenate values of a column within a group.
Parameters
- $column_name : string
-
The name of the column to concatenate
- $alias : string|null = null
-
The alias for the resulting concatenated column, optional
- $separator : string = ','
-
The separator to use between concatenated values, default is ','
- $distinct : bool = false
-
If true, returns only distinct values; default is false
- $order : string|array<string|int, mixed> = null
-
The order in which concatenated values should appear
Tags
Return values
static —HOUR()
Extract the hour from a DATETIME column
public
static HOUR(string $column_name[, string|null $alias = null ]) : static
This function generates a SQL HOUR expression to extract the hour from a DATETIME column.
Parameters
- $column_name : string
-
The name of the DATETIME column
- $alias : string|null = null
-
The alias for the resulting hour, optional
Tags
Return values
static —IFNULL()
Create an IFNULL SQL statement
public
static IFNULL(string $column_name1, string $column_name2[, string|null $alias = null ]) : static
This function generates a SQL IFNULL expression to return the first non-null value from two columns.
Parameters
- $column_name1 : string
-
The first column name
- $column_name2 : string
-
The second column name
- $alias : string|null = null
-
The alias for the resulting expression, optional
Tags
Return values
static —ISNOTNULL()
Create an IS NOT NULL SQL statement
public
static ISNOTNULL(string $column_name) : static
This function generates a SQL IS NOT NULL expression to check if a column is not null.
Parameters
- $column_name : string
-
The column name to check for not null
Tags
Return values
static —ISNULL()
Create an IS NULL SQL statement
public
static ISNULL(string $column_name) : static
This function generates a SQL IS NULL expression to check if a column is null.
Parameters
- $column_name : string
-
The column name to check for null
Tags
Return values
static —MAX()
Find the maximum value of a column
public
static MAX(string $column_name[, string|null $alias = null ]) : static
This function generates a SQL MAX expression to find the maximum value of a column.
Parameters
- $column_name : string
-
The column name to find the maximum value
- $alias : string|null = null
-
The alias for the resulting maximum value, optional
Tags
Return values
static —MIN()
Find the minimum value of a column
public
static MIN(string $column_name[, string|null $alias = null ]) : static
This function generates a SQL MIN expression to find the minimum value of a column.
Parameters
- $column_name : string
-
The column name to find the minimum value
- $alias : string|null = null
-
The alias for the resulting minimum value, optional
Tags
Return values
static —MINUTE()
Extract minutes from a DATETIME column
public
static MINUTE(string $column_name[, string|null $alias = null ]) : static
This function generates a SQL MINUTE expression to extract minutes from a DATETIME column.
Parameters
- $column_name : string
-
The column name to extract minutes from
- $alias : string|null = null
-
The alias for the resulting minutes, optional
Tags
Return values
static —MONTH()
Extract month from a DATE or DATETIME column
public
static MONTH(string $column_name[, string|null $alias = null ]) : static
This function generates a SQL MONTH expression to extract the month from a DATE or DATETIME column.
Parameters
- $column_name : string
-
The column name to extract the month from
- $alias : string|null = null
-
The alias for the resulting month, optional
Tags
Return values
static —NEXT()
Generate SQL to find the next value in a sequence (MAX + 1)
public
static NEXT(string $field, string $table_name[, mixed $condition = null ][, string $alias = null ][, string $operator = 'AND' ][, string $id = 'id' ]) : static
Used to find the next ID in a table.
Parameters
- $field : string
-
The field name to find the maximum value
- $table_name : string
-
The table name
- $condition : mixed = null
-
(optional) WHERE condition for the query
- $alias : string = null
-
(optional) Alias for the resulting field, null means no alias
- $operator : string = 'AND'
-
(optional) Logical operator like AND or OR
- $id : string = 'id'
-
(optional) Key field name
Tags
Return values
static —NOW()
Returns the current date and time as a SQL function NOW().
public
static NOW([string|null $alias = null ]) : static
Parameters
- $alias : string|null = null
-
Optional alias for the NOW() function result in SQL. If provided, formats the SQL as NOW() AS
$alias.
Return values
static —POSITION()
Searches for a substring in a string and returns its position. If not found, returns 0; indexing starts from 1.
public
static POSITION(string $substr, string $str[, string|null $alias = null ], int $pos) : static
Parameters
- $substr : string
-
The substring to search for. If it's a field name, it should be enclosed in ``.
- $str : string
-
The original string to search within. If it's a field name, it should be enclosed in ``.
- $alias : string|null = null
-
Optional alias for the result of the LOCATE() function in SQL. If provided, formats the SQL as LOCATE(...) AS
$alias. - $pos : int
-
Optional starting position for the search. Defaults to 0 (search from the beginning).
Tags
Return values
static —quoteValue()
Quotes and prepares a value for use in SQL queries, handling various data types and formats.
public
static quoteValue(string $column_name, mixed $value, array<string|int, mixed> &$values) : string|int
Updates the $values array with bind parameters for prepared statements.
Parameters
- $column_name : string
-
The column name or identifier to associate with the value.
- $value : mixed
-
The value to quote and prepare for the query.
- $values : array<string|int, mixed>
-
Reference to an array to store bind parameters for prepared statements.
Tags
Return values
string|int —RAND()
Generates a random number.
public
static RAND([string|null $alias = null ]) : static
Parameters
- $alias : string|null = null
-
Optional alias for the RAND() function result in SQL. If provided, formats the SQL as RAND() AS
$alias.
Tags
Return values
static —SECOND()
Extracts the seconds from a DATETIME column.
public
static SECOND(string $column_name[, string|null $alias = null ]) : static
Parameters
- $column_name : string
-
The name of the DATETIME column to extract seconds from.
- $alias : string|null = null
-
Optional alias for the SECOND() function result in SQL. If provided, formats the SQL as SECOND(...) AS
$alias.
Tags
Return values
static —strValue()
Creates a SQL string literal by wrapping the given value in single quotes ('').
public
static strValue(string $value) : static
Parameters
- $value : string
-
The string value to be wrapped in single quotes.
Return values
static —SUM()
Calculates the sum of values in a selected column.
public
static SUM(string $column_name[, string|null $alias = null ][, bool $distinct = false ]) : static
Parameters
- $column_name : string
-
The name of the column to sum. If it's a field name, it should be enclosed in ``.
- $alias : string|null = null
-
Optional alias for the SUM() function result in SQL. If provided, formats the SQL as SUM(...) AS
$alias. - $distinct : bool = false
-
Optional. If true, sums only distinct values in the column. Defaults to false, summing all values in the column.
Tags
Return values
static —text()
Returns the SQL command as a string.
public
text([string|null $key = null ]) : string
If $sql is null, returns :$key for binding purposes.
Parameters
- $key : string|null = null
-
The key used for binding (optional).
Tags
Return values
string —TIMEDIFF()
Calculates the time difference between two datetime columns or values.
public
static TIMEDIFF(string $column_name1, string $column_name2[, string $alias = null ]) : static
Parameters
- $column_name1 : string
-
The first datetime column or value. If it's a column name, it should be enclosed in ``.
- $column_name2 : string
-
The second datetime column or value. If it's a column name, it should be enclosed in ``.
- $alias : string = null
-
Optional alias for the TIMEDIFF() function result in SQL. If provided, formats the SQL as TIMEDIFF(...) AS
$alias.
Tags
Return values
static —TIMESTAMPDIFF()
Calculates the difference between two datetime columns or values in specified units.
public
static TIMESTAMPDIFF(string $unit, string $column_name1, string $column_name2[, string $alias = null ]) : static
Parameters
- $unit : string
-
The unit of time difference to calculate: FRAC_SECOND (microseconds), SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, or YEAR.
- $column_name1 : string
-
The first datetime column or value. If it's a column name, it should be enclosed in ``.
- $column_name2 : string
-
The second datetime column or value. If it's a column name, it should be enclosed in ``.
- $alias : string = null
-
Optional alias for the TIMESTAMPDIFF() function result in SQL. If provided, formats the SQL as TIMESTAMPDIFF(...) AS
$alias.
Tags
Return values
static —WHERE()
Constructs a WHERE clause based on the provided conditions.
public
static WHERE(mixed $condition[, string $operator = 'AND' ][, string $id = 'id' ]) : static
Parameters
- $condition : mixed
-
The condition(s) to build the WHERE clause. Can be:
- A scalar value for simple comparisons.
- An array for more complex conditions:
- [column, operator, value]
- [column, 'IN', array(values)]
- [column, 'NOT IN', Sql::create(subquery)]
- Nested arrays for complex logical conditions.
- $operator : string = 'AND'
-
(optional) The logical operator to combine multiple conditions ('AND' or 'OR'). Defaults to 'AND'.
- $id : string = 'id'
-
(optional) The key field name. Defaults to 'id' if not specified.
Tags
Return values
static —YEAR()
Extracts the year from a DATE or DATETIME column.
public
static YEAR(string $column_name[, string|null $alias = null ]) : static
Parameters
- $column_name : string
-
The name of the DATE or DATETIME column. Should be enclosed in `` if it's a column name.
- $alias : string|null = null
-
Optional alias for the YEAR() function result in SQL. If provided, formats the SQL as YEAR(...) AS
$alias.
Tags
Return values
static —buildWhere()
Constructs SQL WHERE command based on given conditions.
private
buildWhere(mixed $condition, array<string|int, mixed> &$values, string $operator, string $id) : string
Parameters
- $condition : mixed
-
The condition(s) to build into WHERE clause.
- $values : array<string|int, mixed>
-
Array to collect values for parameter binding.
- $operator : string
-
Logical operator (e.g., AND, OR) to combine multiple conditions.
- $id : string
-
Field name used as key in conditions.