簡體   English   中英

為什么我的 postgreSQL 查詢沒有從我的表中找到正確的對象?

[英]Why is my postgreSQL query not finding the correct object from my table?

我得到的東西像 == 不是符號和命令未找到。 通過電子郵件選擇用戶的正確方法是什么? 這是我的表架構:

-- Your database schema goes here --

-- USERS TABLE --
DROP TABLE IF EXISTS users;
CREATE TABLE users(userAccount jsonb);

-- SECRETS TABLE --
DROP TABLE IF EXISTS secrets;
CREATE TABLE secrets(secrets jsonb);

這是我的填充表:

-- USERS TABLE POPULATION --
DELETE FROM users;
INSERT INTO users VALUES ('{"name":"Jack Cooper","password":"banana","email":"jc@gmail.com","role":"admin"}');
INSERT INTO users VALUES ('{"name":"Emmanuel Pena", "password":"potato", "email":"ep@gmail.com", "role": "admin"}');

我試過的查詢是:

SELECT userAccount FROM users WHERE userAccount->'email' ~* $1
SELECT userAccount FROM users WHERE userAccount->'email' LIKE $1
SELECT userAccount FROM users WHERE userAccount->'email' = $1
SELECT userAccount FROM users WHERE userAccount->'email' == $1

您正在使用帶有單箭頭的 json,而過濾您想要使用文本作為json compared to string在任何 SQL 語句中都是無效的,因此請使用以下語法。

SELECT userAccount->'email' FROM users where userAccount->>'email'like'%@gmail.com';

相同的參考: https : //www.postgresqltutorial.com/postgresql-json/

這個怎么運作:

PostgreSQL 提供了兩個原生操作符 -> 和 ->> 來幫助你查詢 JSON 數據。

The operator -> returns JSON object field by key.
The operator ->> returns JSON object field by text.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM