如何設置條件使用別名,其中最後一個順序的自定義字段創建的最大SQL函數和集團用戶的ID來last_order領域:的PostgreSQL在Where條件
SELECT "users"."id", "users"."name", "users"."email", "users"."type", "users"."created_at", MAX(TO_CHAR(order_details.created_at, 'YYYY-MM-dd HH24:MM')) AS last_order
FROM "users"
LEFT JOIN "order_details" on "order_details"."user_id" = "users"."id"
WHERE EXISTS (
SELECT *
FROM "roles"
LEFT JOIN "role_users" on "roles"."id" = "role_users"."role_id"
WHERE "role_users"."user_id" = "users"."id" and "slug" = 'member'
) AND "users"."status" in (0)
AND (LOWER(CAST("users"."id" as TEXT)) LIKE '%%'
OR LOWER(CAST("email" as TEXT)) LIKE '%%'
OR LOWER(CAST("last_order" as TEXT)) LIKE '%%'
OR LOWER(CAST("name" as TEXT)) LIKE '%%')
GROUP BY "users"."id"
ORDER BY "created_at" desc
我總是得到這樣的錯誤:
column "last_order" does not exist
如何使用last_order?我已經把它放在別名中了。
即由數據表生成的查詢,故障原因是我對數據表的搜索功能中使用的別名,我的數據表的代碼就像下面:
columns: [
{ data: 'id', name: 'users.id', class: 'text-center', orderable: false },
{ data: 'email', name: 'email' },
{ data: 'last_order', name: 'last_order', class: 'text-center', orderable: true },
{ data: 'name', name: 'name' },
]
因爲你不能在where子句中使用別名。順便說一句,你不是由很多你不聚合的列組合 –
@juergend那麼,如何將max函數字段包含到where子句? –
使用'having'子句 –