I have a list of Conversations, that come from multiple pages as below:
[
{
"id": 1,
"page_id": 1,
"name": "name 1",
"assigned_user_ids": ["user1"]
},
{
"id": 2,
"page_id": 2,
"name": "name 2",
"assigned_user_ids": ["user2"]
},
{
"id": 3,
"page_id": 1,
"name": "name 3",
"assigned_user_ids": ["user2"]
},
{
"id": 4,
"page_id": 2,
"name": "name 4",
"assigned_user_ids": ["user1"]
}
]
Imagine that "User 1" is calling the query, and User 1 can get all items of page_id = 1 (because user 1 is administrator), but in page_id = 2 he/she
only get the items that has been assigned to, in this case is item with id = 4.
So when user 1 query, what I want to received is:
[
{
"id": 1,
"page_id": 1,
"name": "name 1",
"assigned_user_ids": ["user1"]
},
{
"id": 3,
"page_id": 1,
"name": "name 3",
"assigned_user_ids": ["user2"]
},
{
"id": 4,
"page_id": 2,
"name": "name 4",
"assigned_user_ids": ["user1"]
}
]
This is my existing query in SQL look like:
SELECT * FROM Conversations
WHERE PageId IN (1, 2)
Can any one tell me how to modify my SQL to resolve my problem?
Thank you very much!
You can use OR It will either give you what is on page one, so everything from there, or everything that has been assigned to userId 1. So effectively only the things that have been assigned on every other page.
SELECT * FROM Conversations
WHERE PageId = 1 OR userId = 1
If you want to restrict this to only some pages, you can still use IN.
SELECT * FROM Conversations
WHERE PageId = 1 OR userId = 1 AND pageId IN (2,3,4)
Related
I have the return of a json in the children column of the table. How can I loop over the title values present in the children column?
Example of JSON present in a table row.
[
{
"id": "5CBDE9F2-5D81-4CA9-8302-0908104558D9",
"title": "Title 1",
"Code": "5874"},
{
"id": "9BFC4A6C-9BDC-4C15-B01F-5B87683AE50F",
"title": "Title 2",
"Code": "6582"
}
]
I know using the [key] doesn't work. But like [0] or [2] I get it. How can I interact?
SELECT *
FROM vwJSONFilhos A
WHERE json_value(Filhos,'$[key].title') = 'Title 2'
I have two tables: users table with id, name columns and events table with id, content and userId columns.
I am trying to query a table that return joined information from these two tables with name and events columns where events would represent an array of content fields corresponding to a user.
This is the query I am running:
select
name, group_concat(content) as events
from
users
left join
events on id = userId
group by
userId
order by
id
However rows with null values are not being returned except of just one row. What am I doing wrong?
Users table
[
{
"id": 1,
"name": "Hugo Powlowski"
},
{
"id": 2,
"name": "Jeremy Littel II"
},
{
"id": 3,
"name": "Eleanor King"
},
{
"id": 4,
"name": "Rogelio Jacobson"
},
{
"id": 5,
"name": "Jerald Rowe PhD"
},
{
"id": 6,
"name": "Robyn Tromp"
},
{
"id": 7,
"name": "Norman Zboncak"
},
{
"id": 8,
"name": "Mr. Kristy Orn"
},
{
"id": 9,
"name": "Mrs. Olivia Trantow"
},
{
"id": 10,
"name": "Daniel Lebsack"
}
]
Events table
[
{
"eventId": 3,
"content": "hello",
"userId": 7
},
{
"eventId": 12,
"content": "rulsan berden",
"userId": 1
}
]
Joined table
[
{
"name": "Hugo Powlowski",
"events": "rulsan berden"
},
{
"name": "Jeremy Littel II",
"events": null
},
{
"name": "Norman Zboncak",
"events": "hello"
}
]
You should group by the column in the parent table, not the table being left joined, so that the values will never be null.
So change GROUP BY userid to GROUP BY users.id.
Try to use a nested SELECT, this should return null for the users without any event:
select
u.name,
SELECT(
group_concat(content)
FROM
events
WHERE
userId = u.id
) as events
from
users u
order by
u.id
I am trying to make a system where you can search posts based on tags (using a SQL db).
Example schema:
Post: id, name
Tag: id, name, post_id (foreign_key)
Example Object:
Posts:
{
"id": 1,
"name": "Post1"
}
{
"id": 2,
"name": "Post2"
}
Tags:
{
"id": 1,
"name": "Tag1",
"post_id": 1
}
{
"id": 2,
"name": "Tag2",
"post_id": 1
}
{
"id": 3,
"name": "Tag1",
"post_id": 2
}
Example, I search with Tag1 and Tag2
I want to get back a list with relevance (how many tags matched).
Example:
{
"post_id": 1,
"tag_count": 2
}
{
"post_id": 2,
"tag_count": 1
}
Id started with:
select * from recipes_tag where name in ("tag1", "tag2")
But I can't find a way to count by the post_id to see how many tags each post has in the search.
SQL uses ' to quote strings.
Then you can just use aggregation to count the tags...
SELECT
post_id,
COUNT(DISTINCT name) AS count_of_tags
FROM
recipes_tag
WHERE
name IN ('tag1', 'tag2')
GROUP BY
post_id
If you want to ensure there are at least a certain number of matches, add this to the end...
HAVING
COUNT(DISTINCT name) = 2
First things first:
I'm using PostgreSQL 11.6, compiled by Visual C++ build 1800, 64-bit. :)
Im trying to create a JSON object directly from the database.
My desired result is
{
"1": [],
"2": [],
"3": []
}
Imagine my tables like:
MyIdTable
_id_|__key__
1 test1
2 test2
3 test3
MyKeyValueTable
__id__|__fkidmyidtable__|__value__
1 1 test
2 1 test1
3 2 test2
4 2 test3
Now I create a query
select
json_build_object(
a.id,
json_agg(
b.*
)
)
from "MyIdTable" a
inner join "MyKeyValueTable" b on a.id = b.fkidmyidtable group by a.id
This will get me as result, multiple rows with the desired result:
row 1: {
"1": [{ "id": 1, "fkidmyidtable": 1, "value": "test" }, { "id": 2, "fkidmyidtable": 1, "value": "test1" }]
}
row 2: {
"2": [{ "id": 3, "fkidmyidtable": 2, "value": "test2" }, { "id": 4, "fkidmyidtable": 2, "value": "test3" }]
}
After this I can use json_agg() to create almost my desired result. The issue is that it will create
[ { "json_build_object": {"1": [{ "id": 1, "fkidmyidtable": 1, "value": "test" }, { "id": 2, "fkidmyidtable": 1, "value": "test1" }]}, "json_build_object": { "2": [{ "id": 3, "fkidmyidtable": 2, "value": "test2" }, { "id": 4, "fkidmyidtable": 2, "value": "test3" }] }]
I would like to know if its possible to write a query to merge my created object into one json object like:
{
"1": [{ "id": 1, "fkidmyidtable": 1, "value": "test" }, { "id": 2, "fkidmyidtable": 1, "value": "test1" }],
"2": [{ "id": 3, "fkidmyidtable": 2, "value": "test2" }, { "id": 4, "fkidmyidtable": 2, "value": "test3" }]
}
Thank you very much in advance for taking the time to read :)!
If I followed you correctly, you can add another level of aggregation and use json_object_agg():
select json_object_agg(id, js) res
from (
select a.id, json_agg(b.*) js
from "MyIdTable" a
inner join "MyKeyValueTable" b on a.id = b.fkidmyidtable
group by a.id
) t
type Item struct {
TopicId int `json:"topic_id"`
Topic *Topic `json:"topic,omitempty"`
BotId int `json:"bot_id"`
URL string `gorm:"varchar(250);unique" json:"url"`
Title string `gorm:"varchar(250)" json:"title"`
}
type Topic struct {
Title string `gorm:"varchar(250)" json:"title"`
Items []*Item `json:"items,omitempty"`
}
Here is the two models. I want to query the Topics with each have 5 latest items.
Without the limit for items, I could do this by,db.Model(&Topic{}).Preload("Items").
When I try to add some limit conditions to items:
db.Model(&Topic{}).Preload("Items", func(db *gorm.DB) *gorm.DB {
return db.Order("title DESC").Limit(5)
})
It will return 5 items in total, not 5 items for each topic.
Actual result:
"records": [
{
"id": 4,
"created_on": "2019-08-11T10:28:54.910022Z",
"title": "Topic 1",
},
{
"id": 5,
"created_on": "2019-08-11T10:29:26.952614Z",
"title": "Programming",
},
{
"id": 6,
"created_on": "2019-08-11T10:34:16.040229Z",
"title": "Topic 3",
"items": [
{
"id": 1,
"created_on": "2019-08-27T14:23:17.766055Z",
"topic_id": 6,
"title": "Title One",
},
......
{
"id": 5,
"created_on": "2019-08-27T14:23:17.766055Z",
"topic_id": 6,
"title": "Title five",
}
]
Expected results:
"records": [
{
"id": 4,
"created_on": "2019-08-11T10:28:54.910022Z",
"title": "Topic 1",
},
{
"id": 5,
"created_on": "2019-08-11T10:29:26.952614Z",
"title": "Programming",
"items": [
{
"id": 6,
"created_on": "2019-08-27T14:23:17.766055Z",
"topic_id": 5,
"title": "Title six",
},
......
{
"id": 10,
"created_on": "2019-08-27T14:23:17.766055Z",
"topic_id": 5,
"title": "Title ten",
}]
},
{
"id": 6,
"created_on": "2019-08-11T10:34:16.040229Z",
"title": "Topic 3",
"items": [
{
"id": 1,
"created_on": "2019-08-27T14:23:17.766055Z",
"topic_id": 6,
"title": "Title One",
},
......
{
"id": 5,
"created_on": "2019-08-27T14:23:17.766055Z",
"topic_id": 6,
"title": "Title five",
}
]
The actual sql it generated is SELECT * FROM "item" WHERE "topic_id" IN (6,4,5) DESC LIMIT 5
It's obvious not the results I want, so how should I get the expected result with gorm?
For postgresql only.
type Topic struct {
Title string `gorm:"varchar(250);PRIMARY KEY" json:"title"`
// assume the foreign key between two tables are both Title.
Items []*Item `gorm:"foreignkey:Title;association_foreignkey:Title" json:"items,omitempty"`
}
var topics []Topic
db.Model(&Topic{}).Preload("Items", func(tx *gorm.DB) *gorm.DB {
return tx.Joins(`JOIN LATERAL (
SELECT i.url FROM items i WHERE i.title = items.title ORDER BY i.topic_id DESC LIMIT 5
) AS foo ON foo.url = items.url`)
}).Find(&topics)
You could use lateral join to limit the rows for each different value. After retrieving the rows of topics, gorm then send the following query to get the related rows from items:
SELECT "items".*
FROM "items"
JOIN LATERAL
(SELECT i.url
FROM items i
WHERE i.title = items.title
ORDER BY i.topic_id DESC
LIMIT 5) AS foo ON foo.url = items.url
WHERE ("title" IN (?))