i have 1 table in that i want to reorder the views column using alter function in my php file basically it should be reorder in most views to least views order.and want it to display in same order has altered but it is showing in order of id.
Now here i dont want to use ORDER BY views DESC thats why i am using alter function. because i am using ORDER BY name ASC already in my php file.
TABLE => users
id | name | views
1 | user1 | 700
2 | user3 | 900
3 | user1 | 200
4 | user4 | 900
5 | user4 | 800
6 | user4 | 800
7 | user3 | 900
8 | user4 | 900
9 | user5 | 100
10 | user5 | 100
// this is random table..//
ULtimately i am looking for that when by table is in views ORDERED then i will select data from table and if there are same name then it should be order in views as ALTERED so that i get ouptup like this :
id | name | views
1 | user1 | 900
2 | user1 | 700
3 | user1 | 200
4 | user3 | 900
5 | user4 | 900
6 | user4 | 800
7 | user5 | 900
8 | user5 | 900
9 | user5 | 300
10 | user5 | 100
SQL tables are unordered sets. They have no intrinsic order, and you should never rely on any order returned from queries that don't explicitly state an order by clause. In your case, it seems you need two elements in the ordering - first by name, and then by views:
SELECT *
FROM mytable
ORDER BY name ASC, views DESC
Related
I have a leaderboard which shows the top people who have had the most sales.
It's simple. It just gets the top 25 and displays them
$user = \App\User::orderBy('sales', 'desc')
->take(25)
->get();
So that could end up with
+-------+-------------------+
| pos |user_id| sales |
----------------------------|
| 1 | 1 | 1,293 |
| 2 | 99 | 1,093 |
| 3 | 45 | 985 |
| 4 | 948 | 900 |
| 5 | 39 | 889 |
| 6 | 29 | 887 |
+---------------------------+
Now how can I get the position of the user who is viewing the leaderboard if they are not in the top 25?
If the user id 219 is at the position 935 I want to display something like this at the end
+-------+-------------------+
| pos |user_id| sales |
|---------------------------|
| 935 | 219 | 87 |
+---------------------------+
How is this possible to do efficiently without counting up to the user? (As there are thousands of users)
I suppose you have current user data. So, you don't have to query database to get current user id or sales. So, the main problem is to find count of users who have more sales than current.
So, you need a query like
SELECT COUNT(*) as pos FROM your_table WHERE sales > %current_user_sales%
Translate it to laravel query and add 1 when ouptutting.
I have the following simplified table: (Note we skipped 2nd exam in the exam_id)
+----+---------+-------+
| id | exam_id | score |
+----+---------+-------+
| 1 | 1 | 15 |
| 2 | 1 | 20 |
| 3 | 1 | 68 |
| 4 | 3 | 92 |
| 5 | 3 | 10 |
+----+---------+-------+
I want to write an $sql and some php (I'm using Wordpress, and can use $wpdb) to be able to get the following:
$exam[3]=10
$exam[1]=68
Not that when there are multiple exams, we take the score entry which corresponds to the largest id
And $exam[2] is empty. In words, I'd like to save the last ever exam that the user attempted and show their score.
I've tried using Group By and
Try this
SELECT
*
FROM exams
WHERE exams.id IN (
SELECT
MAX(id)
FROM exams
GROUP BY exam_id
);
I am trying to output a 2D table in php but not able to figure out the sql query for the same
My tables are as below
Students
========
ID | Name
1 | Student1
2 | Student2
3 | Student3
Projects
========
ID | Name
1 | Project1
2 | Project2
3 | Project3
Timesheet
========
ID | Project_id | Student_id | hours
1 | 1 | 1 | 2.5
2 | 2 | 2 | 3
3 | 2 | 1 | 4.5
4 | 1 | 3 | 5
5 | 3 | 3 | 2
6 | 3 | 2 | 1
7 | 3 | 3 | 3.5
8 | 2 | 1 | 6
I want the output as below
| Student1 | Student2 | Student3
Project1 | 2.5 | 0 | 5
Project2 | 10.5 | 3 | 0
Project3 | 0 | 1 | 5.5
Number of students and project will increase and not constant.
Is it possible to write in one sql join query or do I have to write multiple nested php loops to get the above output?
For a big dataset, complex queries using multiple JOINs are basically not good.
"SELECT SUM(hours) AS total FROM Timesheet GROUP BY Project_id, Student_id"
"SELECT ID,Name FROM Students"
"SELECT ID,Name FROM Projects"
I'd rather execute 3 simple queries, then match student and project names in a single loop
I have two tables.. one for a player's session.. and one for the player's character. I can't change how the tables record data.. so I have to work with these. Ultimately, I am trying to create a "who is online" list and need some help figuring out how to sort it all.
account sessions (if a row is present in this list, account is logged in)
----------------------
| accountid | charid |
----------------------
| 1001 | 2001 |
----------------------
| 1004 | 2006 |
----------------------
| 1002 | 2003 |
----------------------
characters (shows all characters active or not)
------------------------------------
| charid | accountid | charname |
------------------------------------
| 1001 | 2001 | user1 |
------------------------------------
| 1002 | 2001 | user2 |
------------------------------------
| 1003 | 2002 | user3 |
------------------------------------
| 1004 | 2002 | user4 |
------------------------------------
| 1005 | 2003 | user5 |
------------------------------------
| 1006 | 2004 | user6 |
------------------------------------
I'm trying to order by online followed by character name. So according to those tables.. I'd want a printout like this:
ONLINE
user1
user3
user6
OFFLINE
user2
user4
user5
What query do I use to print out data like that when it uses two tables and sorting by the second table character names.. and whether or not they're online- two different sorting from two different tables?
If there were character names in the session table.. it'd make this a whole lot easier.. but I don't think I can change that myself. :( Any help?
Try something like this
SELECT charname, IF(sessions.charid IS NULL, 'offline', 'online') as status
FROM characters
LEFT JOIN sessions
ON characters.charid=sessions.charid
AND characters.accountid=sessions.accountid
ORDER BY status DESC, charname ASC
This should give you a two column table with charname and status, online first.
You could get fancier with the GROUP_CONCAT function too
I've got 5 equal tables which look like this:
Table 1
| Name | Points |
|-------|--------|
| User1 | 5 |
| User1 | 2 |
| User1 | 5 |
| User2 | 1 |
| User2 | 7 |
| User2 | 9 |
The rows are inserted when a user answers different questions.
I also have the users in different table.
I want to:
echo User1: 12 points
echo USer2: 17 points
Ho can I make this?
Try this query
SELECT name, SUM(points) as total_points FROM table1 GROUP BY name