So I am using mysql to store likes on Minecraft Server Pages as a way for them to advertise. However, I am storing these likes in a separate table:
id user_id server_id
1 1 1
2 1 24
4 2 22
5 2 22
6 2 1
7 2 4
8 2 5
9 2 6
10 2 17
11 2 18
12 2 21
13 2 24
Insert code:
INSERT INTO likes (user_id, server_id) VALUES ('".$user_id."', '".$server_id."');
But I am currently sorting them by date added (recent first) but I would like to know how to sort them by likes.
You should put a counter column on your servers table (lets say: likes_counter) and then, every time you add a like to the likes table, you increment this counter. Put a index on that column tooo!
INSERT INTO likes (user_id, server_id) VALUES ('".$user_id."', '".$server_id."');
UPDATE servers_table SET likes_counter = likes_counter+1 WHERE server_id = '".$server_id."';
Now, when you select the servers, you only need to do a ordered by likes select:
SELECT * FROM servers_table ORDER BY likes_counter DESC;
Here i'm assuming your table of servers is named servers_table, replace it with the name you gave to the table.
Related
in my project i have to maintain a order of every record and if i delete in between i have to maintain order in a form that order don't have any gape like after 3 next is 4 only... not 5 or 6......my display like.
if i delete multiple record that have Rule ID are 1,3 and 4...then my rule ID 0 remain 0, 2 becomes 1, and 5 becomes 3...like that i want. i am deleting record base on id.
id rileid
1 0
3 1
5 2
6 3
11 4
14 5
how to do that? please help me.
Assume we have these tables
Users table
-----------
id name
-----------
1 xxx
2 yyy
3 ccc
4 bbb
5 aaa
Location table
-------------
id name
-------------
6 Spain
7 Russia
8 Germany
9 USA
Pivot table
------------------------
id user_id location_id
------------------------
1 1 6
2 2 8
3 1 8
4 1 9
5 3 8
What I want to achieve is to sync the data in the pivot table.
So exactly I have post request with array of user ids = [1,5,4] and location_id = 8. So I would get the following result
[updated] Pivot table
-------------------------
id user_id location_id
-------------------------
1 1 6 <-- This one stays
3 1 8
4 1 9
6 5 8 <-- Added
7 4 8 <-- Added
...we deleted the row with location_id=8 and user_id=2 and user=3 because those are not in the users array
How can I do add the new ones, delete the ones that are not in the request, and leave the one that already exists with some functionality, just like Laravel has done it in sync function.
I know that the easyest way is to get all the users that has that specific location_id, delete all, and than insert once again. Is there some workaround or should I do the newbie way :D
Thank you
Not sure if this is the best way to go (actually deleting everything and then inserting the values seems easier and cleaner), but here's one way to do it, if for some reason you want to keep the pivot table ID:
Define a unique index for the table, considering both pivot fields (user_id,location_id).
Insert all data using ON DUPLICATE KEY UPDATE user_id = user_id and location_id = location_id, so that if there's duplicate values, mysql will keep them without modifications and will not throw error.
Implode the Array IDs from the Request and execute a delete from the table, where data is not in the specified values.
This is an expansion of my original question located here:
How do I pull all rows from a table with one unique field and specific values for another field?
I have a table with two fields: user_id and skill_id.
I want to pull out all rows that have a skill_id of a certain number but I have a large number of skill_id's to search for (~30). I was using the self-join suggestion presented in the question linked above but with so many skills to look for, that query is proving extremely slow.
How can I look for a large number of skill_ids without bogging down the query?
EDIT:
Here's an example of what I'm looking for. Using the table below, I want to pull out all rows of users that have skill_id of 10 AND 11 AND 12, etc. (except I'd be looking for more like 30 skills at a time).
TABLE
user_id | skill_id
=====================
1 | 10
1 | 11
1 | 12
1 | 13
2 | 10
2 | 12
2 | 13
3 | 15
3 | 16
4 | 10
5 | 45
5 | 46
If I understand your question well, below query might help you. Assuming (user_id, skill_id) is UNIQUE or PK.
SELECT user_id
FROM tab
WHERE skill_id IN (30 entries)
GROUP BY user_id
HAVING SUM(skill_id IN (30 entries)) = 30;
You can test here. http://www.sqlfiddle.com/#!2/f73dfe/1/0
select user_id
from table
where skill_id IN (10,11,12...)
make suer skill_is is indexed
I have the following table structure
--ID-- --Date-- --Value--
1 2013-1-2 23
2 2013-1-2 11
3 2013-1-3 8
4 2013-1-3 7
As you can see the dates can overlap and I want to output every different date with a summation of the values attributed. So for this it would be.
--Date-- --Total--
2013-1-2 34
2013-1-3 15
Is this even possible with a query or will I have to do some seperate summation?
SELECT Date, sum(Value) as Total
FROM Table
GROUP BY Date
I am working on a project which user registration multiples like this.
1st Month -> 1 user
2nd Month -> 4 users comes under the above user
3rd Month -> 16 users (i.e, 4 users comes under each 4 users above )
4th Month -> 64 users (i.e, 4 users comes under each 16 users above )
eg:
1
2 | 2
8 | 8
32| 32
and continues...
Please give me an advice, how to store this in database.
Thanks in advance.
Assuming user_id is your primary key, create a parent column that contains the user_id of the parent user. For example:
user_id parent
1 NULL
2 1
3 1
4 1
5 1
6 2
...
You will also want to create an index on the parent column so that you can quickly do a reverse lookup (i.e. find all children of a given user).