I've the following tables and I try to select from table1, table2 and table3 and sum them in total:
CREATE TABLE `tbl_cars` (
`carID` int(11) NOT NULL,
`car` varchar(255) NOT NULL,
`product1` int(11) NOT NULL,
`product1_amount` int(11) NOT NULL,
`product2` int(11) NOT NULL,
`product2_amount` int(11) NOT NULL,
`product3` int(11) NOT NULL,
`product3_amount` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `tbl_cars` (`carID`, `car`, `product1`, `product1_amount`, `product2`, `product2_amount`, `product3`, `product3_amount`) VALUES
(1, 'Kamacho 180 PS', 1, 10, 0, 0, 3, 10),
(2, 'Komoda 123 PS', 1, 20, 2, 20, 0, 0);
CREATE TABLE `tbl_orders` (
`orderID` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`ordered` varchar(255) NOT NULL,
`pending` int(11) NOT NULL,
`status` int(11) NOT NULL,
`money` varchar(255) NOT NULL,
`number` varchar(255) NOT NULL,
`idcard` bigint(11) NOT NULL,
`date` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
`account` varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `tbl_orders` (`orderID`, `name`, `ordered`, `pending`, `status`, `money`, `number`, `idcard`, `date`, `account`) VALUES
(1, 'Some_Name', '1', 1, 0, '1000', '1822-25136145', 21474836471, '2020-11-26 05:44:00', '');
CREATE TABLE `tbl_prices` (
`priceID` int(11) NOT NULL,
`product` varchar(255) NOT NULL,
`price` decimal(10,2) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Daten für Tabelle `tbl_prices`
--
INSERT INTO `tbl_prices` (`priceID`, `product`, `price`) VALUES
(1, 'Rolle Stoff', 5.00),
(2, 'Rolle Pappe', 10.00),
(3, 'Eiesenbarren', 15.00),
(4, 'Rolle Stoff123', 20.00),
(5, 'Rolle Pappe123', 25.00),
(6, 'Eiesenbarren123', 30.00);
What i've done is currently this
SELECT product1_amount*price as total FROM tbl_prices, tbl_orders, tbl_cars WHERE orderID = '1' AND ordered = carID AND priceID = product1
UNION
SELECT product2_amount*price as total FROM tbl_prices, tbl_orders, tbl_cars WHERE orderID = '1' AND ordered = carID AND priceID = product2
UNION
SELECT product3_amount*price as total FROM tbl_prices, tbl_orders, tbl_cars WHERE orderID = '1' AND ordered = carID AND priceID = product3
result
total
50
150
So far so good, but i'm looking for this result
total
200
I tried with
SELECT SUM(total) total FROM (
SELECT product1_amount*price as total FROM tbl_prices, tbl_orders, tbl_cars WHERE orderID = '1' AND ordered = carID AND priceID = product1
UNION
SELECT product2_amount*price as total FROM tbl_prices, tbl_orders, tbl_cars WHERE orderID = '1' AND ordered = carID AND priceID = product2
UNION
SELECT product3_amount*price as total FROM tbl_prices, tbl_orders, tbl_cars WHERE orderID = '1' AND ordered = carID AND priceID = product3
)t
GROUP BY total
but this result is still the same result as the first one
total
50
150
Searched now about a hour on google to find a solution and this was my best try to get any result at all what i'm looking for. If there is also a better solution i'd really like to learn more about it.
Anyway for any fix for my question would be amazing.
EDIT:
sample data
http://sqlfiddle.com/#!9/c063a7/4
If you want to find the total the u have to sum all the individual totals not union them
SELECT
ISNULL(( SELECT product1_amount*price as total FROM tbl_prices, tbl_orders, tbl_cars WHERE orderID = '1' AND ordered = carID AND priceID = product1 ),0)
+
ISNULL(( SELECT product2_amount*price as total FROM tbl_prices, tbl_orders, tbl_cars WHERE orderID = '1' AND ordered = carID AND priceID = product2 ),0)
+
ISNULL(( SELECT product3_amount*price as total FROM tbl_prices, tbl_orders, tbl_cars HERE orderID = '1' AND ordered = carID AND priceID = product3),0)
as total;
Related
I need to insert new quantities to the stock table (a1) from source table (a2) and when on insertion closing stock, profit should be calculated. My stock (a1) table as
CREATE TABLE IF NOT EXISTS `a1` (
`id` int NOT NULL AUTO_INCREMENT,
`trs_date` date NOT NULL,
`product_id` int NOT NULL,
`qty_in` decimal(5,2) NOT NULL,
`price_in` decimal(5,2) NOT NULL,
`amount_in` decimal(5,2) NOT NULL,
`qty_out` decimal(5,2) NOT NULL,
`price_out` decimal(5,2) NOT NULL,
`amount_out` decimal(5,2) NOT NULL,
`qty_bal` decimal(5,2) NOT NULL,
`price_bal` decimal(5,2) NOT NULL,
`amount_bal` decimal(5,2) NOT NULL,
`profit` decimal(5,2) NOT NULL DEFAULT '0.00',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO `a1` (`id`, `trs_date`, `product_id`, `qty_in`, `price_in`, `amount_in`, `qty_out`, `price_out`, `amount_out`, `qty_bal`, `price_bal`, `amount_bal`, `profit`) VALUES
(1, '2022-01-01', 1, '10.00', '2.00', '20.00', '0.00', '0.00', '0.00', '10.00', '2.00', '20.00', '0.00'),
(2, '2022-01-01', 2, '20.00', '10.00', '200.00', '0.00', '0.00', '0.00', '20.00', '10.00', '200.00', '0.00'),
(3, '2022-01-02', 1, '5.00', '3.00', '15.00', '0.00', '0.00', '0.00', '15.00', '2.33', '35.00', '0.00'),
(4, '2022-01-02', 2, '0.00', '0.00', '0.00', '5.00', '8.00', '40.00', '15.00', '10.00', '150.00', '-10.00'),
(5, '2022-01-03', 1, '0.00', '0.00', '0.00', '8.00', '9.00', '72.00', '7.00', '2.33', '16.31', '53.36');
and Source Table (a2) as
CREATE TABLE IF NOT EXISTS `a2` (
`id` int NOT NULL AUTO_INCREMENT,
`trs_date` date NOT NULL,
`product_id` int NOT NULL,
`qty_in` decimal(5,2) NOT NULL,
`price_in` decimal(5,2) NOT NULL,
`amount_in` decimal(5,2) NOT NULL,
`qty_out` decimal(5,2) NOT NULL,
`price_out` decimal(5,2) NOT NULL,
`amount_out` decimal(5,2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO `a2` (`id`, `trs_date`, `product_id`, `qty_in`, `price_in`, `amount_in`, `qty_out`, `price_out`, `amount_out`) VALUES
(4, '2022-01-03', 1, '50.00', '4.00', '200.00', '0.00', '0.00', '0.00'),
(5, '2022-01-04', 2, '60.00', '8.00', '480.00', '0.00', '0.00', '0.00'),
(6, '2022-01-05', 1, '0.00', '0.00', '0.00', '10.00', '15.00', '150.00');
Now I need to insert data from a2 to a1 and closing quantity,rate,amount and profit should be calculated.
Closing Quantity = Previous Quantity + Quantity In - Quantity Out
Closing Price (Average Purchase Price)= (Previous Amount Balance + Amount In) / (Previous
Quantity + Quantity In)
Closing Amount = Closing Quantity * Closing Price
Profit = Quantity Out * (Price Out - Closing Price)
My query as follows
insert into a1 (trs_date, product_id, qty_in,price_in,amount_in,qty_out,price_out,amount_out,
qty_bal,price_bal,amount_bal,profit)
with cte_balance as (
select
*,
row_number() over (partition by product_id order by trs_date desc, id desc) as rn
from a1
),
cte_receive as (
select
id, trs_date, product_id, qty_in,price_in,amount_in,qty_out,price_out,amount_out,
qty_bal,price_bal,amount_bal,profit, rn as skip_flag
from cte_balance where rn = 1
union all
select
id, trs_date, product_id, qty_in,price_in,amount_in,qty_out,price_out,amount_out,
0 AS qty_bal,0 AS price_bal,0 AS amount_bal,0 AS profit,0 as skip_flag
from a2
),
cte_new_balance as (
select
id, trs_date, product_id, qty_in,price_in,amount_in,qty_out,price_out,amount_out,
(LAG(qty_bal) over (partition by product_id order by skip_flag DESC, trs_date, id)+qty_in-qty_out) as qty_bal,
0 as amt_bal,
(LAG (amount_bal) over (partition by product_id order by skip_flag DESC, trs_date, id)+ amount_bal)/
(LAG (qty_bal) over (partition by product_id order by skip_flag DESC, trs_date, id)+ qty_bal) as rate_bal,
0 AS new_profit,
skip_flag
from cte_receive
)
select
trs_date, product_id, qty_in,price_in,amount_in,qty_out,price_out,amount_out,
qty_bal as qty_bal,rate_bal AS price_bal,qty_bal*rate_bal AS amount_bal,new_profit AS profit
from cte_new_balance
where skip_flag = 0;
Where cte_balance is to take latest data of each product from table a1, and cte_receive combines the latest data with new data from a2. The query giving unexpected result, my expected result, Table a1 should be as
Get last qty_bal per product (Note to order by trs_date desc, id desc in case more than one row in a day)
Union with receives in a2 (added skip_flag)
Calculate new qty_bal per row (To order by trs_date, id to get data in proper order)
insert into a1 (trs_date, product_id, qty_in, qty_out, qty_bal)
with cte_balance as (
select id, trs_date, product_id, qty_in, qty_out, qty_bal,
row_number() over (partition by product_id order by trs_date desc, id desc) as rn
from a1),
cte_receive as (
select id, trs_date, product_id, 0 as qty_in, 0 as qty_out, qty_bal, rn as skip_flag from cte_balance where rn = 1
union all
select id, trs_date, product_id, qty_in, qty_out, qty_in-qty_out, 0 from a2),
cte_new_balance as (
select id, trs_date, product_id, qty_in, qty_out, qty_bal,
sum(qty_bal) over (partition by product_id order by trs_date, id) as new_bal,
skip_flag
from cte_receive)
select trs_date, product_id, qty_in, qty_out, new_bal as qty_bal
from cte_new_balance
where skip_flag = 0;
Here's my database:
Employee Table:
employee_id int(10) NOT NULL,
firstname varchar(50) NOT NULL,
lastname varchar(50) NOT NULL,
username varchar(15) NOT NULL,
password varchar(25) NOT NULL DEFAULT 'password',
contact_number varchar(13) NOT NULL,
email_address varchar(50) NOT NULL,
position varchar(50) NOT NULL,
teamleader_id int(11) DEFAULT NULL
Service Table:
service_id int(10) NOT NULL,
ticket_id int(10) NOT NULL,
employee_id int(10) NOT NULL,
status varchar(15) NOT NULL,
start_time datetime NOT NULL,
time_in datetime DEFAULT NULL,
time_out datetime DEFAULT NULL,
service_notes varchar(500) DEFAULT NULL
Query:
SELECT * FROM employee AS e
LEFT JOIN service AS s ON e.employee_id = s.employee_id
WHERE (s.status IS NULL OR s.status = 'Completed')
AND e.teamleader_id = ?
EDITED:
I want to select all employees except the ones with service.status = 'Ongoing'
You just have to add DISTINCT to your query :
SELECT DISTINCT e.* FROM employee e LEFT JOIN service s ON e.employee_id = s.employee_id
WHERE(s.status is null OR s.status = 'Completed') and teamleader_id = 3
it filters duplicated
Assuming you just want a list of employees that have completed a service (excluding those who have not and only showing one service per employee)
SELECT employee.*, COUNT(service.status)
FROM employee, service
WHERE service.employee_id = employee.employee_id
AND ( service.status IS NULL OR service.status = 'Completed' )
AND teamleader_id = 1
GROUP BY employee.employee_id;
Or If you do want to list the employees who have not completed any service
SELECT employee.*, COUNT(service.status)
FROM employee LEFT JOIN service ON service.employee_id = employee.employee_id
WHERE ( service.status IS NULL OR service.status = 'Completed' )
AND teamleader_id = 1
GROUP BY employee.employee_id;
or if you want all except where service.status = 'Ongoing'
SELECT employee.*, COUNT(service.status)
FROM employee LEFT JOIN service ON service.employee_id = employee.employee_id
WHERE employee.employee_id NOT IN ( SELECT DISTINCT service.employee_id FROM service WHERE service.status = 'Ongoing')
AND teamleader_id = 1
GROUP BY employee.employee_id;
Tested in SQL Fiddle
CREATE TABLE employee ( employee_id INT(9) PRIMARY KEY, teamleader_id INT NOT NULL, name VARCHAR(99) NOT NULL );
CREATE TABLE service ( id INT(9) PRIMARY KEY, employee_id INT(9) NOT NULL, status VARCHAR(99) NOT NULL );
INSERT INTO employee VALUES (1, 1, 'Bob');
INSERT INTO employee VALUES (2, 1, 'Alice');
INSERT INTO service VALUES (1, 2, 'Complete');
INSERT INTO service VALUES (2, 2, 'WIP');
INSERT INTO service VALUES (3, 2, 'Ongoing');
I have some SQL below the problem I have is both leads (cosmic_leads) show with the same lead stage. I'm finding it hard to explain so I will post a SQL dump at the bottom so you can see table structures etc.
Here is the SQL:-
SELECT
cl.*,
clt.stage_name AS stagename,
cls.date AS moddate,
cls.time AS modtime,
cls.comments
FROM
cosmic_leads cl
INNER JOIN
cosmic_leads_stages cls
ON
cls.lead_id = cl.id
INNER JOIN (
SELECT MAX(id) as id
FROM cosmic_leads_stages
GROUP BY site, lead_id
) clsid
ON
cls.id = clsid.id
INNER JOIN
cosmic_leads_types clt
ON
clt.type = cl.type AND clt.site = cl.site AND clt.stage = cls.stage
WHERE
cls.date >= 20140701 AND cls.date <= 20140723
GROUP BY
cl.id, cl.site
ORDER BY
cls.date DESC LIMIT 0, 10
Using this SQL I get the following:-
[
{
'modtime' => '145717',
'sale' => '1',
'name' => undef,
'moddate' => '20140723',
'comments' => undef,
'username' => 'aap',
'site' => '1',
'handler' => undef,
'stagename' => 'Closed - Success',
'id' => 'lead1',
'type' => 'Website'
},
{
'modtime' => '145717',
'sale' => '0',
'name' => undef,
'moddate' => '20140723',
'comments' => undef,
'username' => 'aap',
'site' => '2',
'handler' => undef,
'stagename' => 'Closed - Success',
'id' => 'lead1',
'type' => 'Website'
}
];
The stage name for lead1 on site 2 should read "New", can anyone see what the issue is?
Thanks
(SQL dump below)
--
-- Table structure for table cosmic_leads
CREATE TABLE IF NOT EXISTS `cosmic_leads` (
`id` varchar(128) NOT NULL,
`type` varchar(96) NOT NULL,
`site` int(3) NOT NULL,
`username` varchar(96) NOT NULL,
`sale` varchar(48) NOT NULL DEFAULT '0',
`name` varchar(96) DEFAULT NULL,
`handler` varchar(96) DEFAULT NULL,
PRIMARY KEY (`id`,`site`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table cosmic_leads
INSERT INTO `cosmic_leads` (`id`, `type`, `site`, `username`, `sale`, `name`, `handler`) VALUES
('lead1', 'Website', 1, 'aap', '1', NULL, NULL),
('lead1', 'Website', 2, 'aap', '0', NULL, NULL);
--
-- Table structure for table cosmic_leads_stages
CREATE TABLE IF NOT EXISTS `cosmic_leads_stages` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`lead_id` varchar(128) NOT NULL,
`site` int(3) NOT NULL,
`stage` int(3) NOT NULL,
`date` int(8) NOT NULL,
`time` int(6) NOT NULL,
`comments` varchar(128) DEFAULT NULL,
`comments_internal` varchar(128) DEFAULT NULL,
`extra` varchar(128) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `lead_id_site_stage` (`lead_id`,`site`,`stage`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
--
-- Dumping data for table cosmic_leads_stages
INSERT INTO `cosmic_leads_stages` (`id`, `lead_id`, `site`, `stage`, `date`, `time`, `comments`, `comments_internal`, `extra`) VALUES
(8, 'lead1', 1, 1, 20140723, 145701, NULL, NULL, NULL),
(9, 'lead1', 1, 9, 20140723, 145717, NULL, NULL, NULL),
(10, 'lead1', 2, 1, 20140723, 145724, NULL, NULL, NULL);
--
-- Table structure for table cosmic_leads_types
CREATE TABLE IF NOT EXISTS `cosmic_leads_types` (
`id` int(12) NOT NULL AUTO_INCREMENT,
`type` varchar(128) NOT NULL,
`site` int(3) NOT NULL,
`stage` int(3) NOT NULL,
`stage_name` varchar(128) NOT NULL,
`status` int(2) NOT NULL,
`amount` decimal(10,2) NOT NULL DEFAULT '0.00',
PRIMARY KEY (`id`),
UNIQUE KEY `type` (`type`,`site`,`stage`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;
--
-- Dumping data for table cosmic_leads_types
INSERT INTO `cosmic_leads_types` (`id`, `type`, `site`, `stage`, `stage_name`, `status`, `amount`) VALUES
(4, 'Website', 1, 1, 'New', 1, '0.00'),
(5, 'Website', 1, 9, 'Closed - Success', 4, '0.00'),
(6, 'Website', 1, 10, 'Closed - Failed', 5, '0.00'),
(10, 'Website', 2, 1, 'New', 1, '0.00'),
(11, 'Website', 2, 9, 'Closed - Success', 4, '0.00'),
(12, 'Website', 2, 10, 'Closed - Failed', 5, '0.00');
--
-- Constraints for dumped tables
--
-- Constraints for table cosmic_leads_stages
ALTER TABLE `cosmic_leads_stages`
ADD CONSTRAINT `cosmic_leads_stages_ibfk_1` FOREIGN KEY (`lead_id`, `site`) REFERENCES `cosmic_leads` (`id`, `site`) ON DELETE CASCADE;
You have 2 cosmic_leads records, each of which have 3 records on cosmic_leads_stages.
Those 6 are joined to the max id of each site / lead_id from cosmic_leads_stages, bringing it down to 4 records. 2 for each record on cosmic_leads:-
ID TYPE SITE USERNAME SALE NAME HANDLER MODDATE MODTIME COMMENTS STAGE
lead1 Website 2 aap 0 (null) (null) 20140723 145724 (null) 1
lead1 Website 1 aap 1 (null) (null) 20140723 145724 (null) 1
lead1 Website 2 aap 0 (null) (null) 20140723 145717 (null) 9
lead1 Website 1 aap 1 (null) (null) 20140723 145717 (null) 9
You then join those 4 records against cosmic_leads_types based on type, site and stage and all 4 have a match, resulting in 4 records.
You then use GROUP BY cl.id, cl.site to reduce this to 1 records for each id / site. For each id / site it has found a match on Closed - Success and on New . The GROUP BY forces it to randomly pick one of these in each case (which one it choses is not defined).
As you current query goes it is producing what is to be expected.
However I think to get the results I think you want you need to include site in the join of cosmic_leads and cosmic_leads_stages
SELECT
cl.*,
clt.stage_name AS stagename,
cls.date AS moddate,
cls.time AS modtime,
cls.comments
FROM cosmic_leads cl
INNER JOIN cosmic_leads_stages cls
ON cls.lead_id = cl.id
AND cls.site = cl.site
INNER JOIN
(
SELECT MAX(id) as id, site, lead_id
FROM cosmic_leads_stages
GROUP BY site, lead_id
) clsid
ON cls.id = clsid.id
AND cls.site = clsid.site
AND cls.lead_id = clsid.lead_id
INNER JOIN cosmic_leads_types clt
ON clt.type = cl.type AND clt.site = cl.site AND clt.stage = cls.stage
WHERE cls.date >= 20140701 AND cls.date <= 20140723
ORDER BY cls.date DESC LIMIT 0, 10
I have a mysql table, the structure of that table is as follows:
CREATE TABLE IF NOT EXISTS `salary_log` (`salary_id` int(11) NOT NULL AUTO_INCREMENT, `salary` int(11) NOT NULL, `name` varchar(200) NOT NULL, `create_date` date NOT NULL, PRIMARY KEY (`salary_id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
INSERT INTO `salary_log` (`salary_id`, `salary`, `name`, `create_date`) VALUES (1, 5000, 'a', '2013-05-02'), (2, 8000, 'a', '2013-05-08'), (3, 4500, 'b', '2013-05-10'), (4,6000, 'c', '2013-05-08'), (5, 8000, 'b', '2013-05-17');
From the above table i need to fetch the latest records of each employee. I tried below query :
SELECT * FROM `salary_log` GROUP BY `name` ORDER BY `create_date` DESC;
But it returns be wrong data. Can someone please help me in this.
Try this:
SELECT s1.*
FROM `salary_log` AS s1
INNER JOIN
(
SELECT name, MAX(create_date) AS maxDate
FROM salary_log
GROUP BY `name`
) AS s2 ON s1.name = s2.name
AND s1.create_date = s2.maxDate;
Between two dates I need to display "today result".
CREATE TABLE IF NOT EXISTS `maf_game_stats` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` varchar(100) NOT NULL,
`sessionid` varchar(100) NOT NULL,
`gid` int(11) NOT NULL,
`qid` int(11) NOT NULL,
`result` varchar(50) NOT NULL,
`accesstime` datetime NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `maf_game_stats` (`id`, `userid`, `sessionid`, `gid`, `qid`, `result`, `accesstime`) VALUES
(1, 'FYEJHQWSHXMV263', '35sd797n57plmreb5ecssuhhh2', 1, 5, '0', '2013-03-30 13:34:03'),
(2, 'FYEJHQWSHXMV263', '35sd797n57plmreb5ecssuhhh2', 1, 1, '0', '2013-03-30 13:34:07'),
(3, 'FYEJHQWSHXMV263', '35sd797n57plmreb5ecssuhhh2', 1, 10, '1', '2013-03-30 13:34:12'),
(4, 'FYEJHQWSHXMV263', '35sd797n57plmreb5ecssuhhh2', 1, 2, '1', '2013-03-30 13:34:17'),
(5, 'FYEJHQWSHXMV263', '35sd797n57plmreb5ecssuhhh2', 1, 16, '1', '2013-03-30 13:34:23');
My query is:
SELECT a.gid, a.qid, SUM(a.result = 1) correct, SUM(a.result = 0) incorrect FROM maf_game_stats a
INNER JOIN
(
SELECT USERID, gid, QID, MIN(ACCESSTIME) min_date
FROM maf_game_stats
GROUP BY USERID, gid, qid
) b ON a.USERID = b.USERID AND
a.gid = b.gid AND
a.qid = b.qid AND
a.ACCESSTIME = b.min_date
WHERE a.gid ='1'
AND a.ACCESSTIME BETWEEN CAST('2013-03-29' AS DATETIME) AND CAST('2013-03-30' AS DATETIME)
GROUP BY a.gid, a.qid
If I understand your question correctly, I think you just need to filter by current date:
WHERE a.gid ='1'
AND DATE(a.ACCESSTIME) = CURDATE()
GROUP BY a.gid, a.qid;
or you can use this, that could be faster because is able to make use of an index:
WHERE a.gid ='1'
AND a.ACCESSTIME >= CURDATE() AND a.ACCESSTIME < CURDATE() + INTERVAL 1 DAY
GROUP BY a.gid, a.qid;