SELECT SQL RETURNING NULL - mysql

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 ;

Related

sub query returns more than 1 - issue with passing values into subquery

I am running the following query which keep stating that more then one row is given:
select filestorage.id
from `filestorage`
where (SELECT LEFT(filestorage_inst_data.value, length(filestorage_inst_data.value - 1)) as seconds
FROM filestorage_inst_data
WHERE filestorage_inst_data.parameter = "Time" AND filestorage_inst_data.filestorage_id = filestorage.id) <= 3600
For some reason, the only very first value is passed into the subquery. Also, if I do set a limit within the subquery than the data is fetched fine, it's just I don't see why query would fetch multiple results?
Try this:
SELECT filestorage.id
FROM filestorage f
WHERE EXISTS(SELECT 1 FROM filestorage_inst_data fid
WHERE fid.parameter = 'Time'
AND fid.filestorage_id = f.id
AND CAST(LEFT(fid.value, length(fid.value - 1)) AS UNSIGNED) <= 3600)
You have to pass a specific one row when giving a select statement on where clause. select clause that, the one you using on where clause must return one unique row. for example.
"SELECT * FROM user WHERE role_id=(SELECT role_id FROM user_role WHERE role_name='Admin');"

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 - Comparsion VARCHAR with INT

On my site user registration date stored as timestamp in varchar(20) column. I want to count how many users registered today, i written query:
SELECT COUNT(*) as count FROM `users` WHERE `user_group` = 7 AND `reg_date > 1368993600
1368993600 - today 0:00. This query return me count of all users instead of count registered today. I edited query:
SELECT COUNT(*) as count FROM `users` WHERE `user_group` = 7 AND CAST(`reg_date` AS SIGNED) > 1368993600
But it still not working... How I can count users registered today?
You are deducting something wrong and your error is somewhere else. See this SQL Fiddle, using the same data structure you are mentioning and using your first query without CAST, and you will see it's working fine. Only 3 rows are returned and are the ones that have a value greater than 1368993600.
SELECT COUNT(*) as count
FROM `users`
WHERE `user_group` = 7
AND `reg_date` - timestamp(curdate()) >= 0

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'.

Weird query result

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