If you are using a older MySQL versions like 5.1 - 5.7 , you need to use MySQL user variables as these MySQL versions does not support window functions and or common table expressions ..
I ported the answers of @Akina and @mustaccio and the fiddle of Akina
to a MySQL 5.1+ working query.
Warning as MySQL 5.1 to MySQL 5.7 does not use window functions and ...
It is unclear whether the method in your example is right or best for you, but if you need to query or return the same expression in many different queries, you can add a custom column to the table itself. In MySQL, this is called a generated column:
custom_column VARCHAR(50) AS
You can use exists and not exists
from employee e
where exists (
select * from dependent where ssn = e.ssn and relationship = 'Son'
and not exists (
select * from dependent where ssn = e.ssn and relationship = 'Daughter'
You can also achieve this with a left join
from employee e
left join dependent d
SELECT name, COUNT(item_id)
FROM ( SELECT c.name, i.item_id
FROM categories c
LEFT JOIN item_category ic ON ic.category_id = c.category_id
LEFT JOIN items i ON ic.item_id = i.item_id
SELECT c.name, i.item_id
FROM categories c
LEFT JOIN subcategories sc ON sc.category_id = c.category_id
Use user-defined variable for to convert the date value from dd\mm\YYYY format to the date/datetime datatype value during import:
LOAD DATA INFILE
(field1, field2, ..., @date, ...)
SET datefield = STR_TO_DATE(@date, '%d\/%m\/%Y');
the rows with the lowest prices whose sum of volumes is less or equal to 10% of total volume. I need the average price of those rows
WITH cte AS ( SELECT price, SUM(volume) OVER (ORDER BY price ASC) summ
FROM sourcetable )
WHERE summ <= 0.1 * ( SELECT MAX(summ)
FROM cte )
Depending on the size of the table, this variant of Akina's solution might perform a little better by calculating both running total and full total of volume in one go:
WITH cte AS (
SUM(volume) OVER (ORDER BY price ASC) running,
SUM(volume) OVER (ORDER BY price ASC
RANGE BETWEEN UNBOUNDED PRECEDING AND ...
CREATE TABLE users ( id PRIMARY KEY, ... );
CREATE TABLE groups ( leader_id REFERENCES users (id),
member_id REFERENCES users (id) );
The keys (including primary) and constraints in groups are dependent by:
does the user may be a member of one group only, or of a lot of groups;
does the group leader may be a member of ...
You can create timestamp in SYSTEM timezone with:
FROM_UNIXTIME(timestamp_in_seconds) -- This is a number, not a datetime
To convert back to a timestamp you can use UNIX_TIMESTAMP function.
This sample demonstrate how to generate random datetime based on starting point. It give you only correct timestamps:
select FROM_UNIXTIME(UNIX_TIMESTAMP('2016-03-27 ...
In my situation slave replication coordinates were broken and I fixed this by resetting my slave and connect my slave to master by new coordinates without any data loss.
First, check if your slave gtid_slave_pos is differing from other slaves or master gtid_current_pos:
# on master
How to enumerate records in contacts table within contact_group_id groups (emulate ROW_NUMBER) starting with 0:
COUNT(t2.contact_id) - 1 row_number
FROM contacts t1
JOIN contacts t2 ON t1.contact_group_id = t2.contact_group_id
AND t1.contact_id >= t2....
You don't need all binary logs from the master. Just the ones that your slave hasn't read yet.
On your slave you can do show slave status\G. Look for the lines Master_Log_File and Relay_Master_Log_File. The first is the binary log file, that your master is using, the second is the log file that your slave is currently reading.
You can then delete all ...
The Slave purges old log files on its own. No problem.
The Master does not have a perfect way to get rid of old binlogs.
The Master can't remove an old binlog until all the slaves have copied it to their relay logs. But the Master neither has a list of Slaves, nor does it know where each is in the copying process.
expire_logs_days works pretty well, but ...
what is wrong with this code?
Wrong point is:
WHERE ... Relationship='Son' AND NOT Relationship='Daughter'
The conditions are applied to THE SAME RECORD. So when Relationship is equal to 'Son' then it is not equal 'Daughter' in this record automatically.
The conditions do not check any another record. So if a record where Relationship='Son' exists then ...
As already answered in comments by @Madhur (All the credits goes to him).
You can use having clause to use alias name without group by.
So instead of WHERE it should be HAVING.
column1, column2, SUBSTRING(column2, 4) AS custom_column
HAVING custom_column = 'xyz'
Note: To know the detail reason why alias name can't be ...