I am building an Android app using Back4App(Parse) database.
I have two classes Post and Post_likes. I want to fetch all posts listing from Post table with individual post likes from post_likes table in the same query.
In Post table, I have post details like post_id, user_id and other details.
In Post_likes table, I have user_id who liked the particular post corresponding to post_id.
Post table with post details
Post_likes table with Post_id and user_id
Now, I want to get total post likes of corresponding post and users who like the particular post along with post listing.
I want to fetch total likes of particular post and then show in home screen post listing.
So, how to write the query to combine the two tables.
Any help will be deeply appreciated.
To define a Pointer Column in the DB, From the Back4App's dashboard you can click on "Edit" button for one Class and add a new Column. Select the type "Pointer" and select the Class name you want to point to in the combo list.
In your code simple add the pointer in the corresponding field
(Something like:
ParseObject PostLikes aPostLike;
ParseObject Post thepost
aPostLike.put("pointedPost", thepost);
(See documentation for accurate syntax).
When you query List of PostLike you can add a "where clause" directly on the pointer Field "pointedPost".
You can also include the content of the Post while query all PostLikes with the key words "include":
(see doc here :
http://parseplatform.org/docs/android/guide/#relational-queries ) :
ParseQuery<ParseObject> query = ParseQuery.getQuery("PostLikes");
// Include the post data with each comment
query.include("post");
Related
Table -article-
id |nam|image|id_categor(FK)|
table category
id |categorieName|
I have two tables in the database. The first table has a foreign key to the second table. Can I get name from the second table by the foreign key ?
need a JSON like
"categories":[
{"categorieName":"test1",
"data":[
{
"name":"wallpaper1",
"image":"tv_101.jpg"
},
{
"name":"wallpaper2",
"image":"tv_102.jpg"
}
]
Welcome, You need to provide a better description with a better code so everyone can understand easily.
If I understand your problem properly then you need a join. You can use SQLite rawQuery to get the desired output.
For Example:
private final String JOIN_QUERY = "SELECT * FROM article a INNER JOIN categories b ON a.id=b.other_id WHERE b.property_id=?";
db.rawQuery(MY_QUERY, new String[]{String.valueOf(propertyId)});
EDIT: I still don't get your question but I think you are using PHP
First of all you cannot directly get the JSON from the database. What you need to do is:
Query your required data using PHP
SELECT * FROM categories then loop it in php and get all articles in each category by querying SELECT * FROM articles WHERE cate_id=loopCategoy_Id
And save it in a key value array
Then convert the array into JSON
Send JSON response
Did you try join ?
Joining two tables by common columns will definitely help here
I want to create an application that any user can add an advert (announce) with something that he wants to sell.
This is the structure of the Real-time Database in Firebase:
- advert
- category
- {uid}
- title
- price
- description
- location
I want to let the user to filter the adverts by category, title, and price.
Currently, I can filter the adverts only by title like this:
FirebaseDatabase.getInstance().getReference()
.child("adverts")
.orderByChild("title")
.startAt("tv")
.limitToFirst(20)
I have 3 questions:
How can i add more "and clauses"? Or how should i structure the nodes to filter more values?
How should I sort the values that i receive from the server in ascending order or descending order depending on uid?
Seems that the method startAt("abc") is not fetching only the children that has contains in the title "tv". Am i doing something wrong? How should I fetch only the children that contains the string "tv"?
On firebase, there is no way to add "AND clauses". You must structure your data in a way that matches your filtering needs.
I believe firebase already sorts the data by uid.
If you want to fetch only the children that contains the string "tv", you should use this query:
FirebaseDatabase.getInstance().getReference().child("adverts").orderByChild("title").startAt("tv").endAt("tv").limitToFirst(20);
I suggest that you watch the Firebase Database for SQL Developers Series. It has the answers for the 3 of your questions
I have some problems with relations in Parse, I don't know why I can't do a relation fine.
my app: a user can create posts, and look posts from other users, a user can add a favorite post to a list, and then see the list of their favorites. I think I need relations many to many for do it.
I have a button "add favorite" , a class "favorites" and a class "posts"
favorites has userId(current user) , and post(favorite post)
posts has namepost,description,category
I tried this.
ParseObject fav = ParseObject.create("favorites");
fav.put("userid",ParseUser.getCurrentUser());
ParseRelation relation = posts.getRelation("objectId");
fav.put("post",relation);
fav.saveInBackground();
But never saves . I also I tried to create a column in Parse "User" class but never works. Some suggestions?. Thanks.
Ok for answer my own question. I created a class Favorites with two columns, user, post
user: ParseUser.getCurrentUser()
post : obj.getobjectId
If I want to retrieve the favorites, first I search for them in a query and save it in a list.
I am using Parse for one of my android application and struggling for a query to get output.
Application Parse DB has three table which is as described below-
User: user related information with unique object Id
Post: Contains Post related information with unique object Id and UserId(Pointer to user table) who create the post
Like: UserId(Pointer to user table) represent who is Liking the post, PostId(Pointer to post table) which post is liking and unique
object Id
Now I want to make a single compound query for All post for a specific user along-with total number of likes to corresponding post. Please suggest a solution/query for that
When a user creates a post, assign the user objectID to some post identifier property in the post table.
To retrieve all posts from a user, a query like this may help:
query.whereEqualTo("objectID", currentUser.objectID)
If you're just concerned about the like count, rather than each user who has liked the post, you may want to have an Integer value in the post table that is incremented after a user presses the like button on that post.
You should create your own webHook.
Following this exemples here you can easily learn and develop your own stuff, you can even add some triggers to your application!
https://parse.com/docs/cloudcode/guide#cloud-code-cloud-functions
https://parse.com/docs/cloudcode/guide#cloud-code-beforesave-triggers
If User table doesn't have Post<.Relation> column
I suggest you to read about how to join queries.
f.g. make a query1 to get the user, then another query2 to get all posts with whereMatchesQuery('UserColumnInPostTable', query1) function.
Now, you are going to get all posts of a specific user efficiently.
.
If User table have Post<.Relation> column
this is the easiest way
Just get the user object and get the relation object, then get the query, and find in background
In my application ,am work with a large database.Nearly 75000 records present in a table(totally 6 tables are there).i want to get a data from three different table at a time.i completed that.but the search process was slow.how can i optimise the searching process?
You might want to consider using the full-text search engine and issuing SELECT...MATCH queries instead. Note that you need to enable the FTS engine (it's disabled by default) and create virtual tables instead of regular tables. You can read more about it here.
Without being able to see the table structure (or query) the first thing I'd suggest is adding some indexes to the tables.
Lets say you have a few tables like:
Author
id
last_name
first_name
Subject
id
name
Book
id
title
author_id
subject_id
and you're wanting to get all the information about each of the books that an author with last_name="Smith" and first_name="John" wrote. Your query might look something like this:
SELECT * FROM Book b
LEFT JOIN Subject s
ON s.id=b.subject_id
LEFT JOIN Author a
ON a.id=b.author_id
WHERE a.last_name='Smith'
AND a.first_name='John';
There you'd want the last_name column in the Author table to have an index (and maybe first_name too).