I have one Master Table with Client_ID and other details of Client. The Second Table is a daily records tabel linked to master Table on Client_ID and has additional column for recording current date. Hence, the Client_ID in the second table has repetitie records based on each date.
I am trying to Select such Records with Client_ID From Master Table for Which Records of Current Date do not exist.
My attempted SQL Statement is as follows:-
SELECT `Client_ID`, `Client_RFID_Number`
FROM ciom_master AS a
WHERE (`Client_Active` ='Y' OR `Client_Active` ='y')
AND CURDATE() NOT EXISTS (
SELECT (`Client_Check_Out`)
FROM `cio_master` AS b
WHERE a.Client_ID = b.Client_ID )
AND CURDATE() NOT EXISTS (
SELECT (`Client_Check_In`)
FROM `cio_master` AS c
WHERE a.Client_ID = c.Client_ID )
I am unable to resolve this statement error. Please help.
You need to move the CURDATE() inside the NOT EXISTS queries:
SELECT `Client_ID`, `Client_RFID_Number`
FROM ciom_master AS a
WHERE (`Client_Active` ='Y' OR `Client_Active` ='y')
AND NOT EXISTS (
SELECT (`Client_Check_Out`)
FROM `cio_master` AS b
WHERE a.Client_ID = b.Client_ID
and Client_Check_Out = CURDATE() )
AND NOT EXISTS (
SELECT (`Client_Check_In`)
FROM `cio_master` AS c
WHERE a.Client_ID = c.Client_ID
and Client_Check_In = CURDATE())
Related
Basically I have a table all customer will have a default row with customer_group_id=0
But some of this customer will belong to customer_group_id=1
When that happen a new row is created for customer with customer_group_id=1 therefore now I have 2 rows for same customer but different customer_group_id.
Now when I fetch the data I need first to select * from customer table where customer_group_id =1 but if doesn't exist give me then with customer_group_id = 0 which is the default, and continue until it returns all data.
Anyone know the best way to achieve this fast?
UPDATE: screen shoot show 2 rows with same customer_id different customer_group_id:
I need to one or the other no both so hierarchy is: if customer_group_id=1 exist then return that row and ignore the ohter otherwise return default which is customer_group_id=0
My full query:
SELECT `main_table`.*, `secondTable`.* FROM `customer` AS `main_table`
LEFT JOIN `customer_group` AS `secondTable` ON main_table.customer_id = secondTable.customer_id
WHERE (secondTable.customer_group_id = '1' )
AND (`secondTable`.`is_active` = '1')
If you have only two groups, then aggregation is simple:
select customer_id, max(customer_group_id)
from t
group by customer_id;
In MySQL 8+, you can implement a more customer prioritization using row_number():
select t.*
from (select t.*,
row_number() over (partition by customer_id order by customer_group_id desc) as seqnum
from t
) t
where seqnum = 1;
What you can do is fetch all the rows for the customers with customer_group_id=1 and then use UNION ALL to fetch the rows of the customers that do not have any row with customer_group_id=1 by using NOT EXISTS:
select * from tablename
where customer_group_id=1
union all
select * from tablename t
where t.customer_group_id=0
and not exists (
select 1 from tablename
where customer_id = t.customer_id and customer_group_id=1
)
For the rows with customer_group_id = 0 verify that there are no rows with customer_group_id = 1 for the same customer_id. You can use a NOT EXISTS subquery:
SELECT c.*, g.*
FROM customer AS c
JOIN customer_group AS g ON c.customer_id = g.customer_id
WHERE NOT EXISTS (
SELECT *
FROM customer_group AS g2
WHERE g2.customer_id = c.customer_id
AND g2.customer_group_id = 1
AND g.customer_group_id = 0
)
I have a mysql table with columns: customer, dateOrder.
One customer can have orders in multiple dates. I want to add a new column with the farthest date order for each customer. So far i tried this:
UPDATE mytable
SET MINDATE = (SELECT min(DATEORDER)
FROM (SELECT *
FROM mytable
GROUP
BY CUSTOMER
) tblTmp
)
, where tblTmp is a temporary table;The problem is that it brings the same date for all my customers (the farthest date in the table). Any ideas?
Use a JOIN to match the original table with the subquery:
UPDATE mytable AS t1
JOIN (SELECT customer, MIN(dateorder) AS mindate
FROM mytable
GROUP BY customer) AS t2 ON t1.customer = t2.customer
SET t1.mindate = t2.mindate
In my system I want to get attendance data of employee from the DB, so I wrote a huge SQL query, and it gives me relevant details. But now I need an updated version of particular results. So I don’t know how to put this query within the update statement.
select * from(
select concat('pre:', date) as date,concat('pre:',employee_no) as employee_no,concat('pre:',name_with_initials) as name,concat('pre:',sign_in_at) as sign_in_at,concat('pre:',sign_out_at) as sign_out_at,emp from( SELECT date, present.employee_no,employee_details.name_with_initials,present.sign_in_at, present.sign_out_at,employee_details.employee_no as emp from (
SELECT employee_no,date,sign_in_at,sign_out_at FROM complete_shifts WHERE date = '2013-06-17' UNION ALL
SELECT employee_no,date,sign_in_at,'00:00:00 ' AS sign_out_at FROM incomplete_shifts WHERE date = '2013-06-17' UNION ALL
SELECT employee_no,date,sign_in_at,'00:00:00 ' AS sign_out_at FROM incomplete_shift_records WHERE date = '2013-06-17'
)as present inner join employee_details on present.employee_no = employee_details.employee_no
) as final_present
union all
select concat('ab:',date)as date,concat('ab:',employee_no)as employee_no,concat('ab:',name_with_initials)as name,concat('ab:',sign_in_at)as sign_in_at,concat('ab:',sign_out_at)as sign_out_at, emp from(
select '2013-06-17' AS date,absent.employee_no,employee_details.name_with_initials,'00:00:00'as sign_in_at , '00:00:00' as sign_out_at,employee_details.employee_no as emp from (
select * from ( SELECT employee_details.employee_no FROM employee_details left outer join resigned_emps on
employee_details.employee_no = resigned_emps.employee_no where resigned_emps.date is null or resigned_emps.date>'2013-06-17'
) as available_emps left outer join (
select employee_no from complete_shifts where date = '2013-06-17' union
select employee_no from incomplete_shifts where date = '2013-06-17' union
select employee_no from incomplete_shift_records where date = '2013-06-17'
) as present on available_emps.employee_no = present.employee_no where present.employee_no is null
) as absent inner join employee_details on absent.employee_no = employee_details.employee_no
) as final_absent
)as final left outer join( SELECT leave.employee_no as lv_emp
FROM leave_dates inner join leave on leave_dates.leave_id = leave.leave_id where leave_dates.date = '2013-06-17')as leave_emps
on final.emp = leave_emps.lv_emp;
With such a large query, you should just put the results in a temporary table and update from that.
create temporary table toupdate as
<your query goes here>;
Now you can investigate the data that will be updated, to be sure that it really is ok.
Next you can do the update as a join:
update table_to_update t join
toupdate
on t.key = toupdate.key
set t.col = toupdate.col
Because you don't give the column or table details, this is just the structure of such a solution.
Instead of "select *", use the appropriate unique field to fetch the records to update, ex EmpID. Finally use this result as inner query result to update query.
Example
Update ... set ... where empid in (your select query goes here)
I'm trying to delete all records from a certain user except the first one, however I'm getting this error:
1093 - You can't specify target table 'History' for update in FROM clause
I don't understand what's the problem and also I found a similar question in SO that proposed this same solution. This is my query:
DELETE FROM `History` WHERE ID = '3' AND Date NOT IN (SELECT min(Date) FROM `History` WHERE ID = '3')
Try converting it to JOIN
DELETE a
FROM History a
LEFT JOIN History b
ON a.Date = b.Date AND
b.ID = 3
WHERE b.date IS NULL AND
a.ID = 3
Before executing the statement above, please do backup the table first.
Here is another way to solve this problem, by wrapping the subquery in another layer of a subquery. MySQL generates the intermediate query, which prevents this error:
DELETE FROM `History`
WHERE ID = '3' AND
Date NOT IN (select mindate
from (SELECT min(Date) as mindate FROM `History` WHERE ID = '3')
)
Please try the below query once
DELETE FROM `History` main WHERE main.ID = '3'
AND main.Date NOT IN
(SELECT min(inn.Date) FROM `History` WHERE main.ID = inn.id)
Please can someone help me, I have a table of users and another table with users and date time (this is a log file and multiple dates exist per user). I need to take the most recent date from the log table and insert it into the first table next to the same user.
This is what I have but its not working:
INSERT INTO tb1 n (DT)
SELECT w.DT
FROM tb2 w
WHERE w.User = n.User
ORDER BY w.DT DESC
limit 1
you don't need to use INSERT statement here since there are already records present on your table. But instead UPDATE it with JOIN
UPDATE tb1 a
INNER JOIN
(
SELECT user, MAX(DT) maxDT
FROM tb2
GROUP by user
) b ON a.user = b.user
SET a.DT = b.maxDT