Weird query result - mysql

I got the following table :
And i need to get the earliest time, per user, per date out of my database. So i have the following query :
SELECT * FROM `taskdate_user` WHERE `taskdate_time` IN
(
SELECT min(`taskdate_time`)
FROM `taskdate_user`
WHERE `taskdate_time` BETWEEN '2013-01-21' AND '2013-01-28'
GROUP BY date_format(taskdate_time, "%Y-%m-%d"), user_id
)
ORDER BY `taskdate_time` ASC
Which results in :
Why does it return double '2013-01-21'? And it goes well with the other dates.
The database i'm using is :
MySQL version: 5.1.66-0ubuntu0.10.04.3 through PHP extension MySQLi

You are grouping also by user_id, and as a result, you have 2 entries for that date

Related

SELECT SQL RETURNING NULL

I'm having trouble performing a select that returns me the last data added in the database by its date.
SELECT * FROM
WHERE Prize
WHERE dataPremio = (SELECT MAX (dataPremio) FROM Prize)
AND idLoteria =?
AND idHerary = ?;
This command returns only the data that was entered today. If I didn't insert some data today, I'd like to returns from yesterday. But it's returning null.
Any tips?
I found the solution using
SELECT * FROM Premio WHERE idLoteria=? AND idHorario=? ORDER BY dataPremio DESC LIMIT 1 ;

In PhpMyadmin Count rows issue with mysql

I added below query in phpmyadmin
SELECT id,u_id,count(u_id) As cnt
FROM `pager`
WHERE type = 'profile' group by u_id
Query Executed.
I got below output showing (0- 30 results) total 24000 records found
I tried same query in different format
SELECT count(u_id) As cnt,id,u_id
FROM `pager`
WHERE type = 'profile' group by u_id
Query Executed.
I got below Query Executed Successfully.SHowing just 30 records , No Pagination dropdown.I can't click id and u_id field heading to sort..Someone please help what is the issue..I think both query should execute 24000 records..Please advise
PhpMyAdmin display default results as per it's settings which you can change from Number of rows : dropdown value in the interface.
If you need to display all your result, then you must add limit clause in your query to display all records
SELECT id,u_id,count(u_id) As cnt
FROM `pager`
WHERE type = 'profile' group by u_id
LIMIT 0, 999999
Otherwise, it will display 30 resuts (or whatever default PhpMyAdmin settings)
NOTE: To sort out the results, add Order by clause in your query then the query would look like following:
SELECT id,u_id,count(u_id) As cnt
FROM `pager`
WHERE type = 'profile' group by u_id
ORDER BY `id` DESC
LIMIT 0, 999999
Hope, it helps you.

MySQL Function only once with group by

I have the following statement:
SELECT user_id, myMysqlFunction(user_id) FROM users_to_days GROUP BY user_id;
The table stores one row per user and day.
The problem is that MySql calls the Function "myMysqlFunction(user_id)" for every row and groups the rows after that.
My target is to execute the Function just once (for performance reasons).
I tried the following state without success:
SELECT user_id, IF(`date` = min(`date`),myMysqlFunction(user_id),0) FROM users_to_days GROUP BY user_id;
MySql still executes the statement for every row.
If I write a static date (for example '2014-09-01') - it works:
SELECT user_id, IF(`date` = '2014-09-01',myMysqlFunction(user_id),0) FROM users_to_days GROUP BY user_id;
But I don't know the min. date for the user at the time of execution. (its not the same for every user)
you could try a subquery:
SELECT user_id, myMysqlFunction(user_id)
FROM (SELECT user_id
FROM users_to_days
GROUP BY user_id);
Edit: comment got split up, I wanted to say:
I meant: try $date = date('Y-m-d');, that will get the time the user runs it. Put that in min($date), and it should be working.

Why does MySQL query not return newest row

I got specific query:
SELECT *
FROM stats
WHERE mod_name = 'module'
GROUP BY domain
ORDER BY addition_date DESC
I want to retrive, the newest value for every domain. I know there is a domain x.com value with date 2014-02-19.
However, this query returns me row with date: 2014-01-06
That's quite simple query... why it does not take group by domains and take only newest value?
Am I missing something?
The order by takes place after the group by. That is why your query does not work. Here is a way to get what you want:
SELECT s.*
FROM stats s
WHERE mod_name = 'module' and
not exists (select 1
from stats s2
where s2.mod_name = s.mod_name and
s2.addition_date > s.addition_date
)
ORDER BY addition_date DESC;
To get the best performance, create an index on stats(mod_name, addition_date).

mySQL - INSERT query that matches the same records as this SELECT query?

I've got a select query I'm using to pick out contacts in my DB that haven't been spoken to in a while. I'd like to run an INSERT query to enter in a duplicate note for all the records that are returned with this select query... problem is I'm not exactly sure how to do it.
The SELECT query itself is likely a bit of a convoluted mess. I basically want to have the most recent note from each partner selected, then select ONLY partners that haven't got a note from a certain date and back... the SELECT query goes:
SELECT * FROM
(
SELECT * FROM
(
SELECT
partners.partners_id,
partners.CompanyName,
notes.Note,
notes.DateCreated
FROM
notes
JOIN
partners ON notes.partners_id = partners.partners_id
ORDER BY notes.DateCreated DESC
) AS Part1
GROUP BY partners_id
ORDER BY DateCreated ASC
) AS Part2
WHERE
DateCreated <= '2013-01-15'
How would a run an INSERT query that would only go into the same records as this SELECT?
The insert would enter records such as:
INSERT INTO notes
(
notes_id,
partners_id,
Note,
CreatedBy,
DateCreated
)
SELECT
UUID(),
partners.partners_id,
'Duplicated message!',
'User',
'2013-02-14'
FROM
partners
If you want to do this all in SQL, you could use an UPDATE statement.
UPDATE tablename SET note='duplicate' where id in ( your statement here);
Note that in order for this to work 'id' needs to be a column from 'tablename'. Then, your statement has to return a single column, not *. The column returned needs to be the id that will let your update statement know which rows to update in 'tablename'.