Skip to content

条件查询


where()whereAnd()

可通过链式调用的方式来指定多条件的查询,但要注意 whereAnd() 前需要有一个 where()

  • @param field 字段名
  • @param condition 条件,如果是 = 可省略不写
  • @param value
  • (field: string, condition: CONDITION, value: string | number)

单条件查询 where()

import { Db } from 'think-ts-db'
export default defineEventHandler(async (event) => {
    const user = await Db('user', mysqlConfig).where('age', '>', 10).select()
    return user
})
import { Db } from 'think-ts-db'
export default defineEventHandler(async (event) => {
    const user = await Db('user', mysqlConfig).where('age', '>', 10).select()
    return user
})

多条件查询 whereAnd()

import { Db } from 'think-ts-db'
export default defineEventHandler(async (event) => {
    const user = await Db('user', mysqlConfig).where('age', '>', 10)
        .whereAnd('gender', '=', 1)
        .whereAnd('status', 2)
        .select()
    return user
})
import { Db } from 'think-ts-db'
export default defineEventHandler(async (event) => {
    const user = await Db('user', mysqlConfig).where('age', '>', 10)
        .whereAnd('gender', '=', 1)
        .whereAnd('status', 2)
        .select()
    return user
})

最终的SQL语句为:

SELECT * FROM user WHERE age = 10 AND gender = 1 AND status = 2
SELECT * FROM user WHERE age = 10 AND gender = 1 AND status = 2

条件对照表 condition


条件说明
=相等
!=不等于
<>不等于
>大于
>=大于等于
<小于
<=小于等于

备案号:冀ICP备20015584号-2