mysql order by asc issue - mysql

I have a application where we need to get the record order by asc but the value with zero record will be in last.
Have anyone help me regarding.
Here is my table structure
id name priority
1 abc 3
2 xyz 6
3 aaa 0
4 bbb 1
5 ccc 1
6 ddd 0
7 fff 2
want something like this
id name priority
1 bbb 1
2 ccc 1
3 fff 2
4 abc 3
5 xyz 6
6 aaa 0
7 ddd 0

SELECT *
FROM mytable
ORDER BY priority = 0, priority
SQLFiddle demo.

Try the following SQL:
SELECT * FROM (SELECT * FROM Table1 WHERE PRIORITY > 0 ORDER BY PRIORITY) a
UNION
SELECT * FROM Table1 WHERE PRIORITY = 0
SQL FIDDLE DEMO

Related

how can i sort data by rows? [MYSQL]

my database
id
title
create_by
1
aaa
2
2
bbb
1
3
ccc
3
4
ddd
2
5
eee
2
6
fff
3
what i want. sort by row that have same most value.
id
title
create_by
1
aaa
2
4
ddd
2
5
eee
2
3
ccc
3
6
fff
3
2
bbb
1
Thank you very much.
WITH cte AS ( SELECT id,
title,
create_by,
COUNT(create_by) OVER (PARTITION BY create_by) cnt
FROM src_table )
SELECT id,
title,
create_by
FROM cte
ORDER BY cnt, id;

How to get row occurrence value in MySQL?

I have a following table
id name amount
1 aaa 1000
2 bbb 1500
3 ccc 1700
4 ddd 2000
5 aaa 1400
6 aaa 1700
7 bbb 1800
What I need is one more column to display the occurrence value based on the name as follows
id name amount occurrence
1 aaa 1000 1
2 bbb 1500 1
3 ccc 1700 1
4 ddd 2000 1
5 aaa 1400 2
6 aaa 1700 3
7 bbb 1800 2
You can do it with a correlated subquery:
SELECT id, name, amount,
(SELECT COUNT(*)
FROM mytable AS t2
WHERE t2.name = t1.name AND t2.id <= t1.id) AS occurrence
FROM mytable AS t1
Demo here

How to fetch all rows with group by value in mysql?

I have a table say table1 which has 3 columns like id, name and age.
Records available in table1,
id name age
-------------------
1 xxx 12
2 yyy 21
3 zzz 12
4 aaa 19
5 ccc 21
6 fff 12
I need result like this,
id name age same_age_count
-----------------------------------
1 xxx 12 3
2 yyy 21 2
3 zzz 12 3
4 aaa 19 1
5 ccc 21 2
6 fff 12 3
This is what my expected result.
Note:
same_age_count is the count value of repeated age in the table.
I have tried this query,
select *, count(age) as same_age_count from table1 group by(age);
id name age same_age_count
-----------------------------------
1 xxx 12 3
2 yyy 21 2
3 aaa 19 1
but it returns 3 records only, please give me a query for my expected result..
Thank you.
Try this:
SELECT T1.*,T2.same_age_count
FROM TableName T1
JOIN
(SELECT age,Count(age) as same_age_count
FROM TableName
GROUP BY age) T2
ON T1.age=T2.age
Result:
ID NAME AGE SAME_AGE_COUNT
1 xxx 12 3
2 yyy 21 2
3 zzz 12 3
4 aaa 19 1
5 ccc 21 2
6 fff 12 3
See result in SQL Fiddle.
If you are using mysql 8 or above you can use window functions.
select t1.*, count(t1.age) over (partition by t1.age) as same_age_count from table1 t1;

mysql select most recent, limit by source

I have a table with the following fields:
id
source_id
title
date
I want to select the 25 most recent items, so SELECT * FROM table ORDER BY date DESC LIMIT 50
The extra requirement is to select only the 3 most recent from every source_id.
So if the records look something like that,
id | source_id | title | date
----+-----------+-------+---------
1 2 aaa 2012-1-1
2 2 aaa 2012-1-2
3 2 aaa 2012-1-3
4 2 aaa 2012-1-4
5 3 aaa 2012-1-5
6 4 aaa 2012-1-6
I want my query to return items 4,3,2,5,6
So just the 3 most recent of every source with an over all limit of 25.
I'm not sure it's clear enough so please ask if you need more details.
Here you go:
SELECT *
FROM your_table t1
WHERE
(
SELECT COUNT(*)
FROM your_table t2
WHERE
t1.source_id = t2.source_id
AND t1.date < t2.date
) < 3
ORDER BY source_id, date DESC
Result:
4 2 aaa 2012-01-04
3 2 aaa 2012-01-03
2 2 aaa 2012-01-02
5 3 aaa 2012-01-05
6 4 aaa 2012-01-06
In plain English: take only rows that have less than 3 newer rows with the same source_id.
NOTE: This could select more than 3 rows per source_id if the third newest date (for the same source_id) happens to be shared by more than one row. Let me know what "3 newest" means in this context if that's a problem...
select * from table where source_id in
(select distinct source_id from table order by date limit 3)
LIMIT 25

Getting only 1 record from each category for each page in pagination

My table structure is as follows
id int
name varchar 50
catid int
Sample data
id name catid
---------------------------------------------------------
1 AAA 1
2 BBB 1
3 CCC 1
4 DDD 2
5 EEE 2
6 FFF 1
7 GGG 2
8 HHH 2
9 III 1
I want query such as it get me 1 row from each category for each page in pagination
Now for 1st Page I need data as
id name catid
---------------------------------------------------------
1 AAA 1
4 DDD 2
Now for 2nd Page I need data as
id name catid
---------------------------------------------------------
2 BBB 1
5 EEE 2
Now for 3rd Page I need data as
id name catid
---------------------------------------------------------
3 CCC 1
7 GGG 2
and so on.
How can I achieve this.
SELECT * FROM table WHERE catid = 1 LIMIT :pageno 1
SELECT * FROM table WHERE catid = 2 LIMIT :pageno 1
I think, this will be simplest query to get required result.
This worked for me (MySQL 5.1 on Linux)
SELECT id, name, catid FROM
(SELECT id, name, catid FROM t WHERE catid=1 LIMIT 2,1) AS t1
UNION (SELECT id, name, catid FROM t WHERE catid=2 LIMIT 2,1)
ORDER BY catid
The 2 in the limit is the page number. Hence the output of this one is for the second page.
Yes, as Pradeep said, a LIMIT clause would be the simplest way to do it in MySQL. You'll still need a little bit of coding in PHP to produce clickable page numbers for your users.