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

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)

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,

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

Select distinct Values from mysql with multiple columns [closed]

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

MySQL, check if result has value, if not take another [closed]

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 have a table structure like this:
Page_id || type || user_id
1 1 0
2 2 0
3 3 0
4 1 1
5 2 1
6 3 1
From this table I would like to get page_id 4,5 and 6.
But I can also have table data like this
Page_id || type || user_id
1 1 0
2 2 0
3 3 0
4 1 1
5 2 1
Then I would like to get page_id 4, 5 and 3.
So I have to get all the types, but with the priority user_id and if there is no record with user_id 1, then take the one with 0
Have tried a lot. I know I can sort it with PHP, but I hope there is a way with MySQL.
Regards Andreas
//////// ANSWER /////////
I got a lot of suggestions, and I haven't tried them all, so I can't tell it they where right or not. But I have accepted an answer, which worked for me.Thank to everybody.
SELECT a.Type, a.Page_ID
FROM table a
INNER JOIN
(SELECT Type, MAX(User_ID) AS User_ID
FROM table
GROUP BY Type ) b
ON a.Type = b.Type AND a.User_ID = b.User_ID
You can execute a SELECT query as follow
SELECT Page_id
FROM table
WHERE user_id != 0
This SQL Fiddle demonstrates the below query:
SELECT DISTINCT
(
SELECT s1.Page_id
FROM myTable AS s1
WHERE m.type = s1.type
ORDER BY s1.Page_id
LIMIT 1
) AS PageID, type,
(
SELECT s2.user_id
FROM myTable AS s2
WHERE m.type = s2.type
ORDER BY s2.Page_id
LIMIT 1
) AS User
FROM myTable AS m
The results are the records where Page_id is 1, 2, and 4. As you can see in both of the sub queries I am ordering by Page_id to make sure the data is pulled from the same record and the first Page_id for that occurrence of the type is selected.
To return only one record unique to a couple columns, you'll want to use the GROUP BY statement. Then for any other column outside of the group by columns, you need to pick an aggregate function so it knows how to summarize the value if it finds multiple records in that group. In this case you want non-zero, so max() would work
SELECT type, max(user_id) as user_id
FROM table
GROUP BY type
how about that?
select page_id,type from (
select page_id,type, user_id from mytable
group by page_id,type, user_id having user_id=max(user_id)
) as x where user_id=1
Will there ever be multiple rows for a page where user_id is not zero? Because if not (if at most you only have one row per page where user_id = 1) then this will work:
SELECT ifNull(t1.page_id,t2.page_id) as page_id, t1.type,
CASE WHEN t2.page_id IS NULL THEN t1.user_id ELSE t2.user_id END as user_id
#start with all rows (including duplicates)
FROM myTable t1
#look for a user_id > 0 for this type
LEFT OUTER JOIN myTable t2 ON t1.type = t2.type AND t2.user_id > 0
WHERE t2.page_id IS NULL # if no record with user_id > 0 found, then no need to filter
# if a record with user_id > 0 was found, then filer out the user_id = 0 record
OR (t2.page_id IS NOT NULL AND t1.user_id > 0)
See in SQLFiddle: http://sqlfiddle.com/#!2/ba877/5

MySQL Updating column based on other values [closed]

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

Categories