I have some vertices of class 'Product' which can be 'viewed', or 'ordered' by class 'User'..
My problem is that I want to write SQL query or MATCH command to get all Product which is 'viewed' by specific user, but it hasn't been ordered by any user..
How to check the relationship 'ordered' doesn't exist between 'Product' and 'User' in MATCH command?
Eg:
ProductA <-viewed- UserA ProductB <-viewed- UserA ProductC
<-viewed- UserA ProductD <-viewed- UserA ProductA <-ordered-
UserA ProductD <-viewed- UserB ProductD <-ordered-
UserB
Input: User A
Output: ProductB, ProductC
Thanks,
Try this:
MATCH {CLASS:Product, AS:pdt, WHERE: (in().size() == 1 and inE().#class = "viewed" and in().name contains "UserA")} RETURN pdt.name
this is the output:
UPDATE
MATCH {CLASS:Product, AS:pdt, WHERE: (in("viewed").name contains 'UserA' and in("ordered").size()=0)} RETURN pdt.name
Hope it helps.
Regards
I think you can use this query
select from Product where in("viewed").#rid contains userRid and in("ordered").size()=0
Hope it helps.
Related
I am using Hasura with my Flutter Application..
I have 2 tables: tasks and categories
tasks comprises of id, task_name, category_id, status.
category comprises of id, category_name, user_id, color.
What I want to do is get the name of the category that the task belongs to using the category_id
What I thought of is:
query getTasks($user_id: String!) {
tasks(where: user_id: {_eq: $user_id}}, order_by: {created_at: desc}) {
category_id
name
}
category_by_pk(id: tasks['category_id']){
name
}
}
The part that is tasks['category_id'] being passed as a query variablele is giving an error
Any idea how can I do this?
Thanks in advance
Have you tracked a relationship in Hasura between tasks and categories? Normally with GraphQL you would just traverse the relationship to get information about the related entity:
query getTasks($user_id: String!) {
tasks(where: user_id: {_eq: $user_id}}, order_by: {created_at: desc}) {
name
category { // Just follow the relationship
id
name
}
}
}
I'm trying to figure out how to create a User Entity with a relation on the friends column that contains other User entities. Joined through a join table of userId pairs.
This sort of works:
#Entity('user')
export class User {
#PrimaryGeneratedColumn('uuid')
id: string;
#ManyToMany(
() => User,
(user) => user.friends
)
#JoinTable()
friends: User[];
}
It does create a relation and I can populate the join table with ids and retrieve the data but it seems to be only one way.
Here is the join table:
userId_1 | userId_2
------------+------------
What I mean by one way is that it the linkage appears to be from userId_1 -> userId_2 and not both ways.
Is there any way to improve on this? I'd like to be able to get the relation from either side based on the one row entry
You need to add the other side of the relation. I'm using "followers" instead of "friends" since that reflects clearly the direction each relation has.
This way you can do something like user.followers and user.following
#ManyToMany(() => User, (user) => user.following)
#JoinTable()
followers: User[];
#ManyToMany(() => User, (user) => user.followers)
following: User[];
I have SQL this query:
SELECT f.follower_id, u.fname, u.lname
FROM followers f
INNER JOIN users u ON f.follower_id = u.id
WHERE f.user_id = $user_id
AND u.status = 1
ORDER BY fname, lname
LIMIT 10
I have two models: User and Follower.
A user can have many followers, and each follower has its own user data. I want to be able to get all of a user's followers (who have a status of 1) by doing something like this:
$followers = User::get_followers();
Add this to your User model:
public function followers()
{
return $this->belongsToMany(User::class, 'followers', 'user_id', 'follower_id')
->where('status', 1)
->orderBy('fname')
->orderBy('lname');
}
Then you can access them like this:
$followers = $user->followers;
I have three tables
SalesDetails with columns SalesId, ProductId, Qty, Price etc
SalesPersonDtls with columns SalesId, SalesPersonId, CommPercentage etc
SalesPerson with columns SalesPersonId, firstName, lastName etc
I have second table because one sale can be done by more than one sales person together with split commission.
I have various inputs in search screen like productname, sales date, sales person name etc.
I am making the model class as 'AsQueryable' and add various where conditions and finally the result into a list.
I have sales person's name in search criteria but I don't know how to include this into the search. Can you please help?
Thanks
Peter
Peter
If I get it correct , relation of your business models is like this :
person (n) <-----> (1) Sale (1) <-----> (n) Details
you put sale and person relation in "SalesPersonDtls" and sale and detail relation to "SalesDetails". I think it's better to change your entities a little bit, if you want to get better result as your project getting bigger and more complex.
Your entities should be like this :
Sale
{
List<SalesDetail> details;
List<Person> persons;
...
}
SalesDetail
{
Sale
...
}
Person
{
Sale
name
...
}
Now it's really simple , if you want sales that is related to a personName :
sales.Where(sale => sale.Persons.Any(person => person.PersonName == "your input name"));
UPDATE :
If you can't or don't want to change your models:
first you need to find personId by it'name and then search into your "SalesPersonDtls" and get saleIds.
I have some json docs in cloudant DB like the following records.
{"#dataType":"GroupItem", "name":"aGroupName1", "GroupType":0},
{"#dataType":"GroupItem", "name":"aGroupName2", "GroupType":1, "Users":[{"deviceUUID":"id1", "userName":"user1"},{"deviceUUID":"id2", "userName":"user2"}] }
They are for groups and the users that belong to the group.
I want to do a query to return the groups the login user belongs to.
So when "user1" login, I'd like to see both "aGroupName1" and "aGroupName2" being returned.
The condition is like:
Select group when GroupType == 0 or (GroupType == 1 and userName == loginUser)
Because the userName is in an array in the json, I don't know how to do the query in Cloudant. Appreciate your help!
Jen
You'll need a map function like the following that emits a compound key:
function (doc) {
if (doc["#dataType"] == "GroupItem") {
doc.Users.map(function(user) {
emit([user.userName, doc.name]);
});
}
}
This will let you query by userName and find all of the matching group name values. Add GroupType to that compound key if you also need to query by group type.