I have a question about MySQL. I have a Table with this fields:
WorkerName
Date
HoursWorked
Ok, if I do this Query:
SELECT WorkerName, Date, HoursWorked, SUM(HoursWorked) FROM myTable GROUP BY WorkerName
I have the field grouped by the Worker Name BUT with a only row. I want to show all days worked by this Worker in the same row, and the other Worker in another row.
In PHP actually have a While that shows all days worked, but only shows the Hours of the first day sorted.
You can use GROUP_CONCAT aggregate function:
SELECT
WorkerName,
GROUP_CONCAT(Date) AS dates_worked,
SUM(HoursWorked)
FROM
myTable
GROUP BY
WorkerName
Related
I have MySQL table with employees attendance. first row of a day of employee treating as in time and last row of a day of employee treating as out time. I am trying to select first and last (min time and max time) from attendance table. It should give me two row sets. but my query not giving me as i expecting the result.
Table (Attendance)
My Query
select *, min(attdate) as intime, max(attdate) as outtime from attendance where empid=1
But above query not giving me as expected result. My output should be in below image. Please suggest me the query or give me hint to achieve given output.
this can be done by sub queries in where conditions.
SELECT * FROM attendance AS c WHERE empid=1 and (
attdate=( select min(attdate) from attendance where attendance.empid=c.empid )
or attdate=( select max(attdate) from attendance where attendance.empid=c.empid )
);
Unfortunately, MySQL doesn't offer window functions, so it's a bit more difficult here. You can use exists :
Select * from yourtable t
Where not exists (select 1 from yourtable s
Where t.empid = s.empid and
(s.attndate < t.attndate or s.attndate > t.attndate))
Though it seems you need to add another condition t.date = s.date unless you have only 1 day records stored there
I have a mysql table which have two columns for selecting propouses - tablica and contentid as it shows here
i want to select first five rows sorted by date DESC which have unique combination of both columns - tablica and contentid
how that can happend? I've tried already with combinations of distinct, group_concat and concat but everything i've tried skip some of the rows.
please help.
Hello_ mate
If you don't need other stuff than those two fields you can use the query I did post in the comments below your question.
Otherwise if you need all the fields you can use this query:
SELECT `id`, `tablica`, `contentid`, `ip`, `userid`, MAX(date) as `date`
FROM test2
GROUP BY tablica, contentid
ORDER BY date DESC
LIMIT 5;
using the MAX function we select the latest matching date for this tablica and contentid, if you want to get oldest date use MIN function
Let me know if this works for you.
Good Luck!
I want to display the logs to recent activities page ordered by date. Now I was trying to execute this to my mysql
"SELECT * FROM tracking_log.editlog, tracking_log.deletelog, tracking_log.loginlog, tracking_log.logoutlog ORDER BY time ASC";
but it always says
Column 'time' in order clause is ambiguous
all of the tables have a time column, format by datetime (0000-00-00 00:00:00)
How am I going to fetch them ordered by time?
Thanks in advance!
By which table's time column you want to order?
Assuming you want to order the result set by tracking_log.editlog.time column then the query would look like below:
SELECT
*
FROM tracking_log.editlog, tracking_log.deletelog,
tracking_log.loginlog, tracking_log.logoutlog
ORDER BY tracking_log.editlog.time ASC;
Just in case if all of the time columns in the respective table don't contain NOT NULL values at the same time then you need to use COALESCE I guess.
Query using COALESCE
SELECT
*
FROM tracking_log.editlog, tracking_log.deletelog,
tracking_log.loginlog, tracking_log.logoutlog
ORDER BY
COALESCE(tracking_log.editlog.time , tracking_log.deletelog.time, tracking_log.loginlog.time,tracking_log.logoutlog.time) ASC;
'tracking_log' is your database name, and you're selecting multiple tables from that database, so you need to specify from which table you want to order 'time' by:
select * from tracking_log.editlog, tracking_log.deletelog ORDER BY tracking_log.editlog.time ASC
or whichever table from your database you want to use 'time' from. This will fix the error but won't return any results because you have multiple tables in a SELECT clause without anything relating them together.
You'll need to specify some common columns on which you want to return results rather than getting the wildcard and then UNION the tables to aggregate the results. For example, if you have common columns userID, description and time in all your tables, you could do the following:
(SELECT userID, description, time FROM tracking_log.editlog)
UNION
(SELECT userID, description, time FROM tracking_log.deletelog)
ORDER BY time
I have two tables in my database:
tickets
ticket_updates
each table has a column called ticketnumber which match. there are sometimes multiple rows in ticket_updates where there is only one row in tickets
I want to be able to show the number of rows from tickets where status = 'Completed' but where it has been completed TODAY
for each row in the ticket_updates table there is a datetime column
As there are multiple rows in ticket_updates for each 1 row in tickets it will need to select the latest datetime from ticket_updates too
You should be able to do this with a simple join and a little MySQL date function:
select
count(sub.counter)
from
(
select distinct
ti.ticketnumber as counter
from
tickets ti
join ticket_updates tu
on ti.ticketnumber=tu.ticketnumber
where
ti.status='Completed'
and date(tu.datetime)=curdate()
) sub
If your datetime (Assuming that isn't the actual name) contains date AND time information, you will need to strip out the time component to compare it properly to the value returned by curdate() which is just a date of today.
The MySQL date() function returns just the date component of a date and time.
Edit: Corrected code to account for multiple relationship as correctly pointed out by #mituw16
I am going to assume there is a key linking these two tables. You might try something like this...
select count(*) as TicketCount from tickets
join ticket_updates on ticket_updates.ticketnumber = tickets.ticketnumber
where tickets.status='Completed' and ticket_updates.datetime = CURDATE()
group by ticket_updates.ticketnumber
Is it possible writing SQL to select duplicate sub string of records from a table into single records ? I just want to group the month and year so the result look like this picture:
I try this, but it didn't work.
SELECT DATE, SUBSTRING(DATE, 3, 6) as Addrow FROM dbo.n4abs_premi_olah GROUP BY Addrow
Any advice will be appreciated. Thank you !
SELECT DISTINCT(DATE_FORMAT(date, '%m/%Y')) month
FROM Table
If you actually need to use GROUP BY (because you're also selecting other aggregates), do:
SELECT DATE_FORMAT(date, '%m/%Y')) month, other stuff...
FROM Table
GROUP BY month
Here's one of many ways to do it:
SELECT DISTINCT CONCAT(MONTH(date), '/', YEAR(date)) FROM table;