I have a total number of 10 employees and I have to display the next 3 employees details on my page and I tried the below code.
SELECT *
FROM employee
where empid > 1
and is_active = 1
order
by empid ASC
limit 0 , 3
and I am getting the output 2,3,4 which is correct.
Now my issue is, If the empid is 10 then I am getting the empty row. Is it possible to display the 1,2,3 when the empid is 10 or if the empid is 8 the display 9,10,1
I mean I want it to wrap around from 10 to 1.
empid | Name | Position | Office | Age
1 | Airi | Accountant | Tokyo | 33
2 | Angelica | Officer | London | 47
3 | Ashton | Technical | Francisco| 66
4 | Bradley | Software | London | 41
5 | Brenden | Engineer | Francisco| 28
6 | qodjf | Accountant | Tokyo | 33
7 | Angelica | Officer | New York | 50
8 | Ashton | Technical | Francisco| 30
9 | zxysz | Software | London | 50
10 | Brenden | Engineer | Francisco| 28
I tried to create a table in db-fiddle but I am getting the error. I am using PHPMyAdmin. So i added table here.
Move the condition empid > ? DESC in the ORDER BY clause:
SELECT *
FROM employee
WHERE is_active = 1
ORDER BY empid > ? DESC, empid LIMIT 3
See the demo.
Related
I had a table with name, negeri and year of start service and end service.I want to get the number of years a person served in a state:
ID | Name | State | yearstart | yearend
-----------------------------------------------------------
1 | Tom | Kedah | 2001 | 2002
1 | Tom | Kedah | 2003 | 2007
2 | Anne | Melaka | 2008 | 2012
2 | Anne | Melaka | 2013 | 2018
3 | Bill | KL | 2000 | 2001
I had already tried. but it only calculate the number of years and list all person(with duplicate name and state).
$query = DB::table('itemregistrations')
->join('state', 'itemregistrations.StateID', '=', 'state.StateID')
->select('itemregistrations.name');
if($request->input('negeri_perkhidmatan') != '') {
$query->join('itemregistrationpangkat', 'itemregistrationpangkat.itemRegistrationID', '=', 'itemregistrations.itemRegistrationID')
->where('itemregistrationpangkat.StateID', $request->input('negeri_perkhidmatan'));
}
if(request('tempoh_negeri')) {
$query->whereRaw('yearend - yearstart >= ?', [request('tempoh_negeri')]);
}
The results display:
ID | Name | State | years
---------------------------------------
1 | Tom | Kedah | 1
1 | Tom | Kedah | 4
2 | Anne | Melaka | 4
2 | Anne | Melaka | 5
3 | Bill | KL | 1
The result should be:
ID | Name | State | years
---------------------------------------
1 | Tom | Kedah | 5
2 | Anne | Melaka | 9
3 | Bill | KL | 1
The result displayed in browser is fetched using ajax. I don't put it here.
this will work:
select m.id,m.name,sum(m.duration) from (select ID,Name,yearend-yearstart
as duration from Table1)m group by m.id,m.name;
check
:http://sqlfiddle.com/#!9/9029438/4
SELECT
m.userid,
m.name,
m.state,
SUM(m.yearend - m.yearstart) AS duration
FROM
mytable m
GROUP BY m.userid , m.state , m.name;
I think this should solve your problem.
Try this !
select id,name,state,sum(years) years from table group by ID
Try SUM() on years column, then GROUP BY by ID, Name, and State.
This will aggregate the 'duplicated' rows that you are trying to avoid.
select name, state (max(yearend) - min(yearstart)) as years from state group by name;
+------+--------+------+
| name | state | years |
+------+--------+------+
| Anne | Melaka | 10 |
| Bill | KL | 1 |
| Tom | Kedah | 6 |
+------+--------+------+
I have 2 tables in MS Access with the following values
Customer
id | name
1 | jon
2 | bob
3 | jack
Order
id | amount | date | customer
5 | 50 | 3/10/2017 | 1
4 | 100 | 3/10/2017 | 1
3 | 45 | 2/28/2017 | 2
2 | 10 | 3/10/2017 | 3
1 | 5 | 3/10/2017 | 2
I want to get an output of
name | orderid | amount
jon | 5 | 50
bob | 3 | 45
jack | 2 | 10
I want to get amount of the latest order id per customer, however I keep getting this
name | orderid | amount
jon | 5 | 50
jon | 4 | 100
bob | 3 | 45
bob | 2 | 10
jack | 1 | 5
I used the query designer and have used the function MAX() to the order id, GROUP BY to all columns (MS Access does not allow to group the rows using a single column), DISTINCT and DISTINCTROW, as well as set the query properties "Unique Records" to Yes but the duplicate records still shows.
I have a MySQL database with the following structure:
custodian | counta | countc | countc | total | date
-------------------------------------------------------
ed | 1 | 2 | 3 | 6 | 1/1/2016
ed | 2 | 3 | 5 | 10 | 1/2/2016
ed | 2 | 3 | 6 | 11 | 1/3/2016
ed | 1 | 3 | 5 | 9 | 1/4/2016
fred | 1 | 2 | 3 | 6 | 1/1/2016
fred | 2 | 3 | 5 | 10 | 1/2/2016
fred | 2 | 3 | 6 | 11 | 1/3/2016
fred | 1 | 3 | 5 | 9 | 1/4/2016
How do I return the latest record for a custodian? I've been playing around with this condition where date >= DATE_SUB(NOW(),INTERVAL 59 MINUTE) since the table is updated hourly, but if I update the script twice in an hour, I would return more than one result per custodian.
Any advice?
You need to combine ORDER BY and LIMIT:
SELECT *
FROM yourTableName
WHERE custodian = 123
ORDER BY `date` DESC
LIMIT 1
You could try this
SELECT * FROM tbl ORDER BY date DESC LIMIT 1
The most recent date will be the first record when ordered decendingly, and limiting the select to 1 means you get only the latest record.
here is my data set and current code: http://sqlfiddle.com/#!2/f9a605/3
| id | courseid | username | day | score |
--------------------------------------------
| 1 | 2 | tim | may 5 | 85 |
| 2 | 2 | mike | may 6 | 86 |
| 3 | 2 | tim | may 7 | 82 |
| 4 | 3 | tim | may 8 | 80 |
| 5 | 2 | mike | may 9 | 79 |
| 6 | 2 | joe | may 10| 81 |
I want to select the values for each user's lowest score where courseid=2 ordered by lowest score so result should look like:
| mike | may 9 | 79 |
| joe | may 10 | 81 |
| tim | may 8 | 82 |
my current code looks like this:
SELECT courseid, username, day, MIN(score) FROM result where courseid = '2' GROUP BY username order by MIN(score) limit 10
my current result looks like this:
| mike | may 6 | 79 |
| joe | may 10 | 81 |
| tim | may 5 | 82 |
The days are wrong.
How do I get the correct day corresponding to the minimum score for that person when courseid =2?
This can be done using a self join on 2 conditions one with username and second with the minimum score
SELECT r.courseid, r.username, r.`day`,r.score
FROM result r
JOIN (SELECT username, MIN(score) score
FROM result
where courseid = '2'
GROUP BY username) r1
ON(r.username = r1.username and r.score = r1.score)
order by r.score
limit 10
Also in your expected result tim should have date of may7
Fiddle Demo
You can use a subquery:
select courseid, username, day, score
from result x
where score = (select min(y.score)
from result y
where y.username = x.username
and x.courseid = y.courseid)
and courseid = 2
order by score
Fiddle:
http://sqlfiddle.com/#!2/f9a605/36/0
I have a table "events" like this
id | user_id | date | is_important
---------------------------------------------------
1 | 3 | 01/02/2012 | 0
1 | 3 | 01/02/2012 | 1
1 | 3 | 01/02/2011 | 1
1 | 3 | 01/02/2011 | 1
1 | 3 | 01/02/2011 | 0
Basically, what I need to get is this:
(for the user_id=3)
year | count | count_importants
--------------------------------------------
2012 | 2 | 1
2011 | 3 | 2
I've tried this:
SELECT YEAR(e1.date) as year,COUNT(e1.id) as count_total, aux.count_importants
FROM events e1
LEFT JOIN
(
SELECT YEAR(e2.date) as year2,COUNT(e2.id) as count_importants
FROM `events` e2
WHERE e2.user_id=18
AND e2.is_important = 1
GROUP BY year2
) AS aux ON aux.year2 = e1.year
WHERE e1.user_id=18
GROUP BY year
But mysql gives me an error
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'aux ON aux.year2 = e1.year WHERE e1.user_id=18 GROUP BY year LIMIT 0, 30' at line 10
And i've run out of ideas to make this query u_Uº. Is it possible to do this using only one query??
Thanks in advance
Edit: I think I over-complicated things. Can't you just do this in a simple query?
SELECT
YEAR(`year`) AS `year`,
COUNT(`id`) AS `count`,
SUM(`is_important`) AS `count_importants`
FROM `events`
WHERE user_id = 18
GROUP BY YEAR(`year`)
Here's the big solution that adds summaries :)
Consider using MySQL GROUP BY ROLLUP. This will basically do a similar job to a normal GROUP BY, but will add rows for the summaries too.
In the example below, you see two records for Finland in 2000, for £1500 and £100, and then a row with the NULL product with the combined value of £1600. It also adds NULL rollup rows for each dimension grouped by.
From the manual:
SELECT year, country, product, SUM(profit)
FROM sales
GROUP BY year, country, product WITH ROLLUP
+------+---------+------------+-------------+
| year | country | product | SUM(profit) |
+------+---------+------------+-------------+
| 2000 | Finland | Computer | 1500 |
| 2000 | Finland | Phone | 100 |
| 2000 | Finland | NULL | 1600 |
| 2000 | India | Calculator | 150 |
| 2000 | India | Computer | 1200 |
| 2000 | India | NULL | 1350 |
| 2000 | USA | Calculator | 75 |
| 2000 | USA | Computer | 1500 |
| 2000 | USA | NULL | 1575 |
| 2000 | NULL | NULL | 4525 |
| 2001 | Finland | Phone | 10 |
| 2001 | Finland | NULL | 10 |
| 2001 | USA | Calculator | 50 |
| 2001 | USA | Computer | 2700 |
| 2001 | USA | TV | 250 |
| 2001 | USA | NULL | 3000 |
| 2001 | NULL | NULL | 3010 |
| NULL | NULL | NULL | 7535 |
+------+---------+------------+-------------+
Here's an example the specifically matches your situation:
SELECT year(`date`) AS `year`, COUNT(`id`) AS `count`, SUM(`is_important`) AS `count_importants`
FROM new_table
GROUP BY year(`date`) WITH ROLLUP;
The alias year - year(e1.date) AS year is not visible in JOIN ON clause. Try to use this condition -
...
LEFT JOIN
(
...
) ON aux.year2 = year(e1.date) -- e1.year --> year(e1.date)
...