Select distinct Values from mysql with multiple columns [closed] - php

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

Related

how to sort mysql data and find position? [closed]

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,

query to Ignore the repeated column in leaderboard

i have a problem my database looks like this DATABASE STRUCTURE
i want to sort the lboard and ignore the repeated values
like this REQUIRED OUTPUT
can anyone help me?
You could use GROUP BY to get single result for one user and MAX function to get its maximum score
the query will be like this
SELECT id, exam_id, user_id, MAX(lboard) FROM `leaderb` GROUP BY user_id ORDER BY lboard DESC
SELECT id, exam_id, user_id, MAX( lboard )
FROM wp_watu_takings WHERE exam_id = 3
GROUP BY user_id
ORDER BY MAX( lboard ) DESC

How to echo latest mysql row and some previous column? [closed]

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'];

mysql - one more complex query or two simple [closed]

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)

How can I order by count in mysql when the count need data to calculate from this select statement?

Look at my code, I want the select statement order by the count percentage after I fetch the data from this select statement, obviously, it's not logical. What can I do? Help, appreciate.
<?php
//myslq connection code, remove it because it's not relate to this question
$stm =$db->prepare("SELECT id ,term_count, COUNT(user_id) as count FROM sign WHERE term IN (:term_0,:term_1) GROUP BY user_id ORDER by count DESC");
//trying replace order by count with $combine_count, but it's wrong
$term_0="$term[0]";
$term_1="$term[1]";
$stm->bindParam(":term_0", $term_0);
$stm->bindParam(":term_1", $term_1);
stm->execute();
$rows = $stm->fetchALL(PDO::FETCH_ASSOC);
foreach ($rows as rows) {
$count=$rows['count'];
$term_count_number=$rows['term_count'];
$count_percentage=round(($count/$count_user_diff)*100);
$count_key_match=round(($count/$term_count_number)*100);
$combine_count=round(($count_percentage+$count_key_match)/2);
//issue is here, I want the select statement order by $combine_count
}
?>
SELECT id ,term_count, COUNT(user_id) as `count`
FROM sign
WHERE term IN (:term_0,:term_1)
GROUP BY user_id
ORDER by `count` DESC");
Since "count" is a function, it would be better to put backtics around the non-function "counts", as done above.
GROUP BY should list the field not aggregated. Otherwise, it does not know which id and term_count to fetch. So, depending on what you are looking for,
Either do
SELECT user_id, COUNT(*) as `count` -- I changed this line
FROM sign
WHERE term IN (:term_0,:term_1)
GROUP BY user_id
ORDER by `count` DESC");
or do
SELECT id ,term_count, COUNT(*) as `count`
FROM sign
WHERE term IN (:term_0,:term_1)
GROUP BY id ,term_count -- I changed this line
ORDER by `count` DESC");
SQL Syntax Logic
SELECT column1, count(column1) AS amount
FROM table_name
GROUP BY column1
ORDER BY amount DESC
LIMIT 12

Categories