selecting from table where timestamp is latest - php

i have made a table for an audit trail.
CREATE TABLE IF NOT EXISTS `audit_trail_timer` (
`_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`remaining_duration` varchar(50) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`_id`)
)
now i wanted to select a row where the timestamp is the most recent one.
$query = select * from audit_trail_timer where user_id='$id' and timestamp='';

SELECT top 1 * FROM audit_trail_timer WHERE user_id='$id'
ORDER BY TIMESTAMP DESC;

try this
SELECT * FROM audit_trail_timer WHERE user_id='$id'
ORDER BY TIMESTAMP DESC LIMIT 1

try this,
SELECT * FROM audit_trail_timer WHERE user_id='$id'
ORDER BY TIMESTAMP DESC LIMIT 1

Use
SELECT *
FROM audit_trail_timer
WHERE user_id = '$id'
ORDER BY `timestamp` DESC
LIMIT 1
timestamp is reserved keyword

Related

select 3 items with higher sales

I am wanting to select the 3 biggest selling records with this is my table
CREATE TABLE IF NOT EXISTS `contas` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_service` int(11) DEFAULT NULL,
`data` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=28 ;
the 'id_service' is the main column, the more sales, more records are added with the same 'id_service'.
so how do I do this without using PHP and select in descending order?
I tried this
select id_service, count(*) as id_service
from vendas WHERE id_service is not null
group by id_service order by id_service desc LIMIT 3
You have aliased both columns to the same name. No wonder the query is confused. Try this:
select id_service, count(*) as cnt
from vendas WHERE id_service is not null
group by id_service
order by cnt desc
LIMIT 3;

SQL: How to select one record per day, assuming that each day contain more than 1 value MySQL

I want to select records from '2013-04-01 00:00:00' to 'today' but, each day has lot of value, because they are saving each 15 minutes a value, so I want only the first or last value from each day.
Table schema:
CREATE TABLE IF NOT EXISTS `value_magnitudes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`value` float DEFAULT NULL,
`magnitude_id` int(11) DEFAULT NULL,
`sdi_belongs_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`reading_date` datetime DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1118402 ;
Bad SQL:
SELECT value FROM `value_magnitudes` WHERE `value_magnitudes`.`reading_date` BETWEEN '2013-04-01 00:00:00' AND '2013-04-02 00:00:00' AND (`value_magnitudes`.magnitude_id = 234) LIMIT 1
SELECT value FROM `value_magnitudes` WHERE `value_magnitudes`.`reading_date` BETWEEN '2013-04-02 00:00:00' AND '2013-04-03 00:00:00' AND (`value_magnitudes`.magnitude_id = 234) LIMIT 1
SELECT value FROM `value_magnitudes` WHERE `value_magnitudes`.`reading_date` BETWEEN '2013-04-03 00:00:00' AND '2013-04-04 00:00:00' AND (`value_magnitudes`.magnitude_id = 234) LIMIT 1
SELECT value FROM `value_magnitudes` WHERE `value_magnitudes`.`reading_date` BETWEEN '2013-04-04 00:00:00' AND '2013-04-05 00:00:00' AND (`value_magnitudes`.magnitude_id = 234) LIMIT 1
SELECT value FROM `value_magnitudes` WHERE `value_magnitudes`.`reading_date` BETWEEN '2013-04-05 00:00:00' AND '2013-04-06 00:00:00' AND (`value_magnitudes`.magnitude_id = 234) LIMIT 1
etc ...
I want all in one if possible...
Thank you a lot.
EDIT:
I mean, I have a query per day, but I just want to make a single query from reading_date >= '2013-04-01 00:00:00'c
EDIT2:
I have 64,260 records in that table.value_magnitudes and it takes sooooooooo long to excecute and response that query, and sometimes timeout conection.
To get the first entry for every date you can do
select * from value_magnitudes
where id in
(
SELECT min(id)
FROM value_magnitudes
WHERE magnitude_id = 234
and date(reading_date) >= '2013-04-01'
group by date(reading_date)
)
select * from value_magnitudes
where id in
(
SELECT min(id)
FROM value_magnitudes
WHERE `value_magnitudes`.`reading_date` BETWEEN '$from_selected' AND '$to_selected' and (magnitude_id = 234) group by date(reading_date)
)
using the above example - in case someone needs this - it's searching for invalid records where action should not be 0 if its the first row of the day:
select * from log
where action=0 AND id in
(
SELECT min(id)
FROM log
group by date(ts)
)

Count total values for given month

I have this mySQL statement:
SELECT COUNT(clicks) FROM ads.statz WHERE user_id = '$_SESSION[user_id]'
Works fine it counts all the number of clicks with the given user id. What I am after is being able to group together the numbers for each month as a total. I need this for both Clicks and impressions.
Here is the layout of the DB.
CREATE TABLE `statz` (
`ad_id` int(50) NOT NULL,
`date` datetime NOT NULL,
`clicks` int(50) NOT NULL,
`impressions` int(50) NOT NULL,
`user_id` int(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Thanks!
Use MONTH() and YEAR() with GROUP BY
SELECT
COUNT(clicks)
FROM
ads.statz
WHERE
user_id = '$_SESSION[user_id]'
GROUP BY
MONTH(date),
YEAR(date)

mySQL Query, group by and then order by most recent grouped?

Say I have the following table:
CREATE TABLE `table` (
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
userid INT UNSIGNED NOT NULL,
reference INT,
`datetime` DATETIME
) Engine=InnoDB;
I want to select from the table, group by the reference and order by DATE, but also order by the latest reference entry?
For example:
reference: 79
datetime: 2011-12-31 00:32:30
reference: 77
datetime: 2011-12-31 00:40:30
reference: 77
datetime: 2011-12-31 00:43:30
reference: 77
datetime: 2011-12-31 00:45:30
reference: 78
datetime: 2011-12-31 00:47:30
They should show in this order: 78, 77 (the 00:45 one), 79
I currently have this as my query:
SELECT *
FROM `table`
WHERE `userid` = '" . mysql_real_escape_string($id) . "'
GROUP BY `reference`
ORDER BY `datetime` DESC
How can I get this query to work? So when a reference which already exists gets another entry, it jumps to the top of the list?
Thank you
Try
SELECT id, userid, reference, MAX(datetime) AS datetime
FROM `table` WHERE `userid` = ID
GROUP BY `reference`
ORDER BY `datetime` DESC
you need to specify all the columns near Group By clause.
SELECT id, userid, reference, MAX(datetime) AS datetime
FROM `table` WHERE `userid` = ID
GROUP BY `id`, `userid`, `reference`
ORDER BY `datetime` DESC

Complex Queries - Get people with the most referrals

I am trying to make some sort of SQL Query where I only get the 10 people with the most referrals, but with minimum 1 referral.
My Table looks like this:
CREATE TABLE IF NOT EXISTS `beta_list` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`email` varchar(250) NOT NULL,
`referrer` int(10) NOT NULL,
`referral_code` int(10) NOT NULL,
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
I have tried something like this:
SELECT
email,
referral_code as refcode,
(SELECT COUNT(*) FROM beta_list WHERE referrer=refcode) as referrals
FROM
beta_list
WHERE
referrals > 0
ORDER BY
referrals DESC
LIMIT
10
But it just says "Unknown column 'referrals' in 'where clause'".
I am no sql guru, I am only just beginning to learn more complex sql queries, so any help on how to achieve something like this would be deeply appreciated!
Cheers!
Try this - Add an outer query to extract the results from inner query -
select ref.email, ref.refcode, ref.referrals from
(
SELECT
email,
referral_code as refcode,
(SELECT COUNT(*) FROM beta_list WHERE referrer=refcode) as referrals
FROM
beta_list
) as ref
WHERE
ref.referrals > 0
ORDER BY
ref.referrals DESC
LIMIT
10
Give this a go:
SELECT email,referral_code as refcode,count(*) as referrals
FROM beta_list
WHERE referrer = referral_code
GROUP BY email,referral_code
ORDER BY referrals DESC
LIMIT 10;

Categories