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
Related
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 ;
I've ran into some performance issues with my database structure "or better to say my query instead "
I have a the following table :
http://sqlfiddle.com/#!9/348cb
And following query trying to fetch certain data, and after that trying to check if there are other records matching my conditions, it's all in the following query.
it is working as expected, the only reason that I'm asking this question is that if there is a way I could increase its performance or use another way to get the results.
As you can see, There two ( SELECT )'s which trying to check if there are any other records containing current query data.
SELECT (
SELECT COUNT(*) FROM log AS LIKES
WHERE L.target_account=LIKES.target_account
AND LIKES.type='like'
) as liked,
(
SELECT COUNT(*) FROM log AS COMMENTS
WHERE L.target_account=COMMENTS.target_account
AND COMMENTS.type='follow_back'
) as follow_back,
(
SELECT COUNT(*) FROM log AS FOLLOW_BACK
WHERE L.target_account=FOLLOW_BACK.target_account
AND COMMENTS.type='follow_back'
) as follow_back,
L.*
FROM `log` as L
WHERE `L`.`information` = '".$target_name."'
AND `L`.`account_id` = '".$id."'
AND `L`.`date_ts` BETWEEN CURDATE() - INTERVAL ".$limit." DAY AND CURDATE()
This query takes too much time to fetch the data.
Thanks in advance.
You may be able to rewrite the query, depending on the relationship between target account and account id.
In the meantime, you want indexes. The two you want are instagram_log(target_account, type) and instagram_log(account_id, information, date_ts):
create index idx_instagram_log_1 on instagram_log(target_account, type);
create index idx_instagram_log_2 on instagram_log(account_id, information, date_ts);
SELECT SUM(LIKES) LIKES,SUM(FOLLOW_BACK) FOLLOW_BACK,SUM(COMMENTS) FROM
(
SELECT
CASE WHEN L.type='like' THEN 1 ELSE 0 END LIKES,
CASE WHEN L.type='follow_back' THEN 1 ELSE 0 END FOLLOW_BACK,
CASE WHEN L.type='comments' THEN 1 ELSE 0 END COMMENTS
FROM `log` as L
WHERE `L`.`information` = '".$target_name."'
AND `L`.`account_id` = '".$id."'
AND `L`.`date_ts` BETWEEN CURDATE() - INTERVAL ".$limit." DAY AND CURDATE()
)Z
Try the above query.
Hi i would like to create query which returns status for meters in apartment.
I have tables BR_Apartment, BR_Meter and BR_Parameter_Value.
Default value for meter status is active (1) all history data was stored in BR_Parameter_Value table when status has changed.
I need only last status for meters in apartment or default value.
Current query returns all records from BR_Parameter_Value.
Example of my query on SQL Fiddle
Can someone correct me if I am building my query incorrectly or educate me with a few tips on how to properly accomplish what I want to do?
Edit:
I've updated example based on answer from Gordon which works if date is NOW() but I would like to use same query for history values as well to get meter status based on specific date.
You can use this to get the most recent parameter value from the parameter table (just the addition of the exists statement in the where clause):
SELECT m.*, IFNULL( pva.ParameterValue, 1 ) AS MeterIsActive,
IF( pva.ParameterValue <1, 'meterNotActive', '' ) AS MeterTypeClass
FROM BR_Meter m INNER JOIN
BR_Apartment a
ON ( m.ApartmentID = a.ApartmentID ) LEFT JOIN
BR_Parameter_Value pva
ON ( pva.ForeignID = m.MeterID AND
pva.ParameterDate <= NOW( ) AND
pva.ParameterID =12
)
WHERE m.ApartmentID = 2452 and
(not exists (select 1
from BR_Parameter_value pv2
where pv2.ForeignID = pva.ForeignID and
pv2.parameterid = pva.parameterid and
pv2.ParameterDate > pva.ParameterDate
)
)
I don't know what you mean by default value.
I'm having an odd problem, and I don't have the slightest idea of why it isn't working.
I have the following query that I constructed:
SELECT servers.id, servers.name, servers.address, servers.port, servers.up, servers.down, servers.genre, servers.score, servers.version, servers.country, ROUND( AVG( reviews.average ) , 0 ) AS review
FROM servers
INNER JOIN reviews ON servers.id = reviews.server
ORDER BY servers.score DESC
This query was working fine a few weeks ago. It is meant to get many fields from the "servers" table, and the average field from the "reviews" table where the server in the "reviews" table is the same as the id in the "servers" table.
Like I said, this query was working fine before. Yesterday I noticed that a vital part of my site wasn't working, and I figured out that this query is failing.
I've confirmed that is returning exactly 1 row (when, at the moment, it should be returning 4, because there are 4 entries in the "servers" table.)
This is what phpMyAdmin gives me when I execute that query:
id name address port up down genre score version country review
NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
Could anybody enlighten me? I've come here as a last resort, because I am stuck.
As mentioned in the comments, try changing the INNER JOIN to a LEFT OUTER JOIN which will return servers, regardless if there is a matched row in the review table or not. Also, you didn't post your schema, but double check the reviews.server column in the reviews table, it may be server_id instead. Another issue, you are doing an AVG which is a grouped calculation, but you have no GROUP BY clause, so I would suggest adding it, so your full query should look like:
SELECT servers.id, servers.name, servers.address, servers.port, servers.up, servers.down, servers.genre, servers.score, servers.version, servers.country, ROUND( AVG( reviews.average ) , 0 ) AS review
FROM servers
LEFT OUTER JOIN reviews ON servers.id = reviews.server # might be reviews.server_id
GROUP BY reviews.server
ORDER BY servers.score DESC
More info about GROUP BY functions.
-- Update --
SELECT servers.id, servers.name, servers.address, servers.port, servers.up, servers.down, servers.genre, servers.score, servers.version, servers.country, IFNULL(ROUND(AVG(reviews.average)), 0) AS review
FROM servers
LEFT OUTER JOIN reviews ON servers.id = reviews.server
GROUP BY servers.id
ORDER BY servers.score DESC
I have an event occurring once a day. I have 2 tables:
application
rating
Basically, each application has an avg_score that is given by the average of all the feedbacks given by users that are stored in the table rating in the field score. I wrote an event that once a day refresh this value:
CREATE EVENT MY_DAILY_UPDATE
ON SCHEDULE EVERY 1 DAY STARTS '2011-07-23 23:30:00'
DO
UPDATE application
SET `avg_score`= (SELECT AVG(`score`) as new_score
FROM `rating`
WHERE `ID_APPLICATION` = 1)
WHERE `APPLICATION_ID` = 1
It works, but only for the application with ID = 1, cause i wrote it by myself.
Instead i need my query to update the field avg_score for each application in the table application.
So i think i need to change the value 1 with a variable ID (ex WHERE APPLICATION_ID = ID_VARIABLE).......and this variable should take the id value of each app in the application table (1,2,3.....4 etc).......but i have no idea about how to change my query.....
Change your sub-query to referrence the values in the outer query. (This makes it a correlated sub-query.)
UPDATE application
SET avg_score = (
SELECT AVG(score)
FROM rating
WHERE ID_APPLICATION = application.APPLICATION_ID
)
Alternatively, as you're doing this for "all values", just join on the sub-query...
UPDATE
application
INNER JOIN
(
SELECT ID_APPLICATION, AVG(score) AS score FROM rating GROUP BY ID_APPLICATION
)
AS averages
ON averages.ID_APPLICAITON = application.APPLICATION_ID
SET
application.avg_score = averages.score
UPDATE application
SET `avg_score`=
(SELECT AVG(`score`) as new_score
FROM `rating`
WHERE `ID_APPLICATION` = `application.APPLICATION_ID`)