Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have mysql data of 3 rows (user_id, exam_id, marks).
I want to find each user ranking position based on marks and exam_id .
How will be the SQL query with php code for this. Please help me. Thanks in advance.
Here is partial code
$p = "SELECT * FROM IDTABLE WHERE EID = '$info[EID]'";
$rowp = mysqli_query($conn, $p);
while (($ret = mysqli_fetch_assoc($rowp)) > 0) {
$q = "SELECT * FROM INFOTABLE WHERE EID = '$ret[EID]'";
$rowq = mysqli_query($conn, $q);
while (($retq = mysqli_fetch_assoc($rowq)) > 0) {
$user[$ret['EID'] . $retq['UID']] = $retq['Marks'];
}
}
arsort($user);
var_dump($user);
First of all you should consider a few things:
You should always better search for an already answered question before posting a new one (it's part of stack overflow's code)
Bring more information of what you want, being as clear as possible
Giving more details (for example: you sql structure)
However, I will try to give you a hint - Hope it helps!
For example you want to order your user_id based on their marks:
select user_id from my_table order by "marks" asc;
For example you want to order your user_id based on their marks and exam_id :
select user_id from my_table order by "marks", "exam_id" asc;
For example you want to create a kind of ranking based on users's marks:
SET #rank := 0;
SELECT
*,
#rank := #rank + 1 AS rank
FROM my_table
order by marks ASC;
For example you want to get the ranking number from a specific
SET #rank := 0;
SELECT * from ( select
*,
#rank := #rank + 1
FROM my_table
order by marks ASC ) AS rank where user_id = 2;
Hope this helps! You could get more information about mysql's ranking here
Regards,
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a MySQL table having many rows and my question is,
How to
i) Display rows from MySQL table as the row length is equal to multiples of 3
ii) In other case if row count not equal to multiples of 3 then display the row equal to nearest multiples of 3
for example, if the row length is 5 then display only first 3 rows.
for more clarification,
If mysql_num_rows = 3, display all three rows.
If mysql_num_rows = 5, display any three rows.
If mysql_num_rows = 11, display any nine rows.
Use
$res=mysqli_query(
$con,"SELECT * FROM tbl_name");
$row=mysqli_num_rows($res);//calculating total row count
$lim=$row-$row%3;//deciding limit
$res1=mysqli_query(
$con,"SELECT * FROM tbl_name LIMIT $lim");
//echo '<br>'.$row1=mysqli_num_rows($res1);
while($show=mysqli_fetch_array($res1))
{
echo '<br>'.$show['id'];
}
Execute the query & show your data
MySQL Only
SELECT (COUNT(*)-(COUNT(*)%3)) FROM tbl_name INTO #lim;
SELECT #rownum := 0;
SELECT t.*, #rownum := #rownum + 1 AS rowno FROM tbl_name t WHERE rowno <= #lim;
PHP & MySQL
$res=mysqli_query( $conn, "SELECT COUNT(*) AS cnt FROM tbl_name");
$rec=mysqli_fetch_array($res);
$lim=$rec["cnt"]-($rec["cnt"]%3);
$res=mysqli_query( $conn, "SELECT * FROM tbl_name LIMIT $lim");
You'll need to validate cnt and lim
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am new in php. I have a problem. I want to fetch data from Mysql db and print it on my PHP page. I want to print latest record and some previous data of selected column. For example i have a two tables
table1( id, student_name )
table2: ( id, student_id, English, Math, Physics......., Total Marks, Obtain Marks and grade)
PROBLEM: I want that i can fetch whole new record but i want that i can also fetch previous grade of the student.
Can it is possible? How i Can do it?
With
SELECT * FROM (
SELECT * FROM table2 WHERE (student_id=2) ORDER BY id DESC LIMIT 2
) sub
ORDER BY id ASC
You should be able to select the two last rows.
Edit syntax formatting:
$result = mysql_query($sql);
$rows = mysql_fetch_assoc($result);
echo $rows[0]["grade"]; // new grade
echo $rows[1]["grade"]; // previous grade
You can try with the following:
$query = SELECT t1.*,t2.* FROM table1 as t1
LEFT JOIN table2 as t2 ON t1.id = t2.student_id
where t1.id = '1' ORDER BY t2.student_id desc LIMIT 2
$result = mysql_query($query);
$fetch_record = mysql_fetch_assoc($result);
$grade = $fetch_record[0]['pass-your-column-name'];
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I would like to know which query is better. I want to select some specific records and all count of records in table(but filtered).
Which query is better?
SELECT name, surname,
(select COUNT(messages.id) from messages WHERE `messages`.`archived` = '0' AND `type` IN (0, 1) ) as count
FROM messages JOIN
users
ON users.id = messages.user_id
WHERE messages.archived = 0 AND type = 0 OR type = 1
ORDER BY created_on DESC
LIMIT 3
or
SELECT name, surname
FROM messages JOIN
users
ON users.id = messages.user_id
WHERE messages.archived = 0 AND type = 0 OR type = 1
ORDER BY created_on DESC
LIMIT 3
with
SELECT count(id) as count
FROM messages
WHERE `messages`.`archived` = '0' AND `type` IN (0, 1)
If I have big db I will check this out, but for now I have got a few records.
With single query the count is added to every record.
Thanks and sorry for my english!
After writing this, I realized that the two queries are not the same. So, you should use the version that corresponds the results you want to get. (Do you want the count filtered or not?)
I'm not sure if MySQL will optimize the subquery in the SELECT. However, you can just move it into the FROM clause, where it is only run once:
SELECT `name`, `surname`, m.cnt
FROM `messages` JOIN
`users`
ON `users`.`id` = `messages`.`user_id` CROSS JOIN
(select COUNT(messages.id) as cnt from messages) m
WHERE `messages`.`archived` = '0' AND `type` = 0 OR `type` = 1
ORDER BY `created_on` DESC;
LIMIT 3
I suspect your WHERE clause is incorrect. Do you really intend this?
WHERE `messages`.`archived` = '0' AND `type` IN (0, 1)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
i've one table pages. In this table i am having records pages table where some of the pages having duplicate entries. I want to fetch unique pages from the table on the basis of city_id. Where i m executing this query :-
SELECT distinct pagename FROM tbl_page
WHERE status = '0' AND city_id = '78' order by pagename
This query returning me distinct records as 2608. I want to fetch the id and section id of this pages as well in my query. So when i m executing this query
SELECT distinct pagename,id,section_id FROM tbl_page
WHERE status = '0' AND city_id = '78' order by pagename
At this time it is not returning distinct value. It is returning 3400 records which are duplicates. Can anyone tell me what i m doing wrong?
Any help will be appreciated. Thanks in Advance.
I do not really know what results you are expecting but maybe this is what you want:
SELECT pagename,id,section_id FROM tbl_page
WHERE status = '0' AND city_id = '78'
GROUP BY pagename, id, section
order by pagename
if you like to have unique pagenames you should:
SELECT pagename,id,section_id FROM tbl_page
WHERE status = '0' AND city_id = '78'
GROUP BY pagename
order by pagename
But you may have different id/section_id values for one pagename so i think the value for id and section_id will be the value from the first row where the pagename was found.
DISTINCT will return only distinct rows, so, You could also switch to GROUP BY instead.
SELECT `pagename`, `id`, `section_id` FROM `tbl_page`
WHERE status = '0' AND city_id = '78'
GROUP BY 'pagename', 'section_id', `id`
ORDER BY `pagename` ASC
DISTINCT always works on a single column. Alternatively you can use GROUP BY keyword which can be used on multiple columns. See the below query:
SELECT pagename,id,section_id FROM tbl_page WHERE status = '0' AND city_id = '78' GROUP BY pagename,id,section_id ORDER BY pagename
Try this might be solved :
You need to use distinct over complete rows Like that way :
SELECT pagename,id,section_id,DISTINCT(*) FROM tbl_page WHERE status = '0' AND city_id = '78' GROUP BY pagename,id,section_id ORDER BY pagename
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am building a simple gaming system and I was wondering how to get the users rank without calculating everytime the result in a query(maybe I should run a cron job?).
This is my points table structure at the moment:
+--username--+--rank--+--total_points--+
|######################################|
| Mark | 0 | 700 |
|------------+--------+----------------|
| Luke | 0 | 400 |
+------------+--------+----------------+
How do I update the rank column based on total_points?
Is there a better way to do this? Thank you for your time
You need only one query:
UPDATE
users,
(SELECT
#row:=#row+1 rownum,
username
FROM users,
(SELECT #row := 0) r
ORDER BY total_points DESC) as rank_ord
SET users.rank = rank_ord.row
WHERE users.username = rank.username;
If your table contains an index column (other then username), then change the condition
WHERE users.username = rank.username
to
WHERE users.id = rank.id
to make it faster.
Assuming that the rank depends on total_points you just need to sort query results by total_points DESC and row number is your position in rank. No need to calculate anything. Of course you may have two users with same total_points so if you want to have same rank this will not work, but you can do pretty simple the same approach (pseudo code):
$rows = query with order total_points DESC
$rank = 0;
$last_total_points = -1;
foreach( $rows as $row ) {
if( $row['total_points'] != $last_total_points ) {
$rank++;
$last_total_points = $row['total_points'];
}
update that row with $rank;
}
so at the end, you will get all users ranked with the same rank for equal total_points values too