add_sql_query_condition¶
- pyhelpers.dbms.utils.add_sql_query_condition(sql_query, add_table_name=None, **kwargs)[source]¶
Add a condition to a given SQL query statement.
- Parameters:
sql_query (str) – SQL query statement to which the condition will be added.
add_table_name (str | None) – [Optional] table name to add to each column name in the condition; defaults to
None
.
- Returns:
Updated SQL query statement with the specified conditions.
- Return type:
str
Examples:
>>> from pyhelpers.dbms.utils import add_sql_query_condition >>> query = 'SELECT * FROM a_table' >>> query 'SELECT * FROM a_table' >>> add_sql_query_condition(query) 'SELECT * FROM a_table' >>> add_sql_query_condition(query, COL_NAME_1='A') 'SELECT * FROM a_table WHERE "COL_NAME_1"='A'' >>> add_sql_query_condition(query, COL_NAME_1='A', COL_NAME_2=['B', 'C']) 'SELECT * FROM a_table WHERE "COL_NAME_1"='A' AND "COL_NAME_2" IN ('B', 'C')' >>> add_sql_query_condition(query, COL_NAME_1='A', add_table_name='t1') 'SELECT * FROM a_table WHERE t1."COL_NAME_1"='A''