Exclusion of all rows possible via outer join? - mysql

I'll start off with the schema:
CREATE TABLE CustomersActions (
`caID` int(10) unsigned NOT NULL AUTO_INCREMENT primary key,
`cusID` int(11) unsigned NOT NULL,
`caTimestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
)
CREATE TABLE `Assignments` (
`asgID` int(10) unsigned NOT NULL AUTO_INCREMENT primary key,
`cusID` int(11) unsigned NOT NULL,
`asgAssigned` date DEFAULT NULL,
`astID` int(10) unsigned NOT NULL
)
CREATE TABLE `AssignmentStatuses` (
`astID` int(10) unsigned NOT NULL AUTO_INCREMENT primary key,
`astStatus` varchar(255) DEFAULT ''
)
My original query is:
SELECT DISTINCT
ca.cusID
FROM
CustomersActions ca
WHERE
NOT EXISTS (
SELECT
TRUE
FROM
Assignments asg
NATURAL JOIN AssignmentStatuses
WHERE
asg.cusID = ca.cusID
AND (
DATE_ADD(asgAssigned, INTERVAL 6 DAY) > NOW()
OR astStatus IN('Not contacted', 'Follow-up')
)
)
What this does is select all cusID entries from CustomersActions if said Customer does not have a row in Assignments that is in "Not contacted" or "Follow-up" (for any date range) OR has an assignment of any status from less than six days ago.
I tried writing the same query using LEFT JOIN to exclude from Assignments like so:
SELECT DISTINCT
ca.cusID
FROM
CustomersActions ca
LEFT JOIN (
Assignments asg
NATURAL JOIN AssignmentStatuses
) ON (ca.cusID = asg.cusID)
WHERE
asgID IS NULL
OR DATE_ADD(asgAssigned, INTERVAL 6 DAY) < NOW()
OR astStatus IN('Not contacted', 'Follow-up')
The problem is that it's possible for a customer to have multiple entries in Assignments so a cusID can be selected even if they have an existing row that should force them to be excluded. This makes sense to me, and the NOT EXISTS solves this problem.
What I'm wondering is if there is a way to perform a single query that has the same effect as the query when using NOT EXISTS. That is, a customer should be excluded if they have any rows that satisfy the exclusion condition, not only if all of their rows satisfy the exclusion condition (or if they have none).

Have you tried using NOT IN clause, like:
SELECT DISTINCT
ca.cusID
FROM
CustomersActions ca
WHERE cusID
NOT IN (
SELECT
cusID
FROM
Assignments asg
INNER JOIN AssignmentStatuses ast
ON asg.astID = ast.astID
WHERE
DATE_ADD(asgAssigned, INTERVAL 6 DAY) > NOW()
OR astStatus IN('Not contacted', 'Follow-up')
)

Related

Select child rows by skipping first 5 records based on parent_id

For the following query I get the error This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
SELECT * FROM `wp_dash_competition_versions` _versions
WHERE DATEDIFF(CURRENT_TIMESTAMP, _versions.created_at) > 20 AND
id NOT IN (
SELECT id FROM `wp_dash_competition_versions` WHERE competition_id = _versions.competition_id
ORDER BY created_at LIMIT 5
)
Other SO articles recommend the use of a join, however the join query doesn't apply the limit to a given parent_id.
Is it possible to achieve a single query that selects rows from a child table skipping the first 5 rows for a given parent_id?
I'm developing the query primarily for a delete operation to prune the table. I need the select first to ensure the statement is correct.
CREATE TABLE `wp_dash_competition_versions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`competition_id` bigint(20) unsigned DEFAULT NULL,
`competition_serialised` mediumtext COLLATE utf8mb4_unicode_520_ci,
`created_user_id` bigint(20) unsigned NOT NULL,
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
)
SELECT _version.*
FROM `wp_dash_competition_versions` _version
INNER JOIN (
SELECT competition_id, GROUP_CONCAT(id ORDER BY created_at DESC) _versions
FROM `wp_dash_competition_versions` GROUP BY competition_id
) group_versions
ON _version.competition_id = group_versions.competition_id
AND FIND_IN_SET(id, _versions) > 5
AND DATEDIFF(CURRENT_TIMESTAMP, created_at) > 20
Found in answer https://stackoverflow.com/a/15585351/972457

Mysql Inner join - how to use value of first table column in join clause

In my "bookings" table, each booking has a number of persons and an "event_time" , which is one of three time slots which is bookable.
In my query I am trying to return how many free seats there are left for each restaurant and time slot (event_time number)
I select restaurants and do an INNER JOIN to include the bookings table, but I would need access to the "number_of_seats_max" column from the restaurants table inside the inner join, which does not seem possible.
Here is fiddle.
Tables:
CREATE TABLE `restaurants` (
`id` int(10) UNSIGNED NOT NULL,
`title` text COLLATE utf8mb4_unicode_ci,
`number_of_seats_max` int(11) DEFAULT NULL
);
CREATE TABLE `bookings` (
`id` int(10) UNSIGNED NOT NULL,
`event_date` timestamp NULL DEFAULT NULL,
`event_time` int(11) NOT NULL,
`number_of_persons` int(11) NOT NULL,
`restaurant_id` int(11) NOT NULL
);
The below query works, but in this case I have hard coded "80" instead of the max seats column ( r.number_of_seats_max ). Thats the column I need to use. If you put r.number_of_seats_max instead, you get the error "unknown column".
SELECT r.title, r.number_of_seats_max, innerquery.free_seats_left,
innerquery.num_persons_booked
FROM restaurants r
INNER JOIN(
select
restaurant_id,
SUM(number_of_persons) as num_persons_booked,
(80 - SUM(number_of_persons)) AS free_seats_left // <-- 80 is hard coded
from bookings
WHERE event_date = '2019-07-18'
group by event_time,restaurant_id
ORDER BY free_seats_left DESC
) as innerquery
ON innerquery.restaurant_id = r.id;
How can I solve it?
Do the subtraction in the main query, not the subquery.
SELECT r.title, innerquery.event_time, r.number_of_seats_max,
r.number_of_seats_max - innerquery.num_persons_booked AS free_seats_left,
innerquery.num_persons_booked
FROM restaurants r
INNER JOIN(
select
restaurant_id,
event_time,
SUM(number_of_persons) as num_persons_booked
from bookings
WHERE event_date = '2019-07-18'
group by event_time,restaurant_id
) as innerquery
ON innerquery.restaurant_id = r.id
ORDER BY free_seats_left DESC
I added event_time to the SELECT list of both the subquery and the main query, so you can show the available seats for each time slot.

Counting records in normalized table

Older questions seen
Counting one table of records for matching records of another table
MySQL Count matching records from multiple tables
Count records from two tables grouped by one field
Table(s) Schema
Table entries having data from 2005-01-25
CREATE TABLE `entries` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`ctg` VARCHAR(15) NOT NULL,
`msg` VARCHAR(200) NOT NULL,
`nick` VARCHAR(30) NOT NULL,
`date` DATETIME NOT NULL,
PRIMARY KEY (`id`),
INDEX `msg` (`msg`),
INDEX `date` (`date`)
)
COLLATE='utf8_general_ci'
ENGINE=MyISAM;
Child table magnets with regular data from 2011-11-08(There might be a few entries from before that)
CREATE TABLE `magnets` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`eid` INT(10) UNSIGNED NOT NULL,
`tth` CHAR(39) NOT NULL,
`size` BIGINT(20) UNSIGNED NOT NULL DEFAULT '0',
`nick` VARCHAR(30) NOT NULL DEFAULT 'hjpotter92',
`date` DATETIME NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `eid_tth` (`eid`, `tth`),
INDEX `entriedID` (`eid`),
INDEX `tth_size` (`tth`, `size`)
)
COLLATE='utf8_general_ci'
ENGINE=MyISAM;
Question
I want to get the count of total number of entries by any particular nick(or user) entered in either of the table.
One of the entry in entries is populated at the same time as magnets and the subsequent entries of magnets can be from the same nick or different.
My Code
Try 1
SELECT `e`.id, COUNT(1), `e`.nick, `m`.nick
FROM `entries` `e`
INNER JOIN `magnets` `m`
ON `m`.`eid` = `e`.id
GROUP BY `e`.nick
Try 2
SELECT `e`.id, COUNT(1), `e`.nick
FROM `entries` `e`
GROUP BY `e`.nick
UNION ALL
SELECT `m`.eid, COUNT(1), `m`.nick
FROM `magnets` `m`
GROUP BY `m`.nick
The second try is generating some relevant outputs, but it contains double entries for all the nick which appear in both tables.
Also, I don't want to count twice, those entries/magnets which were inserted in the first query. Which is what the second UNION statement is doing. It takes in all the values from both tables.
SQL Fiddle link
Here is the link to a SQL Fiddle along with randomly populated entries.
I really hope someone can guide me through this. If it's any help, I will be using PHP for final display of data. So, my last resort would be to nest loops in PHP for the counting(which I am currently doing).
Desired output
The output that should be generated on the fiddle should be:
************************************************
** Nick ||| Count **
************************************************
** Nick1 ||| 10 **
** Nick2 ||| 9 **
** Nick3 ||| 6 **
** Nick4 ||| 10 **
************************************************
There might be a more efficient way but this works if I understand correctly:
SELECT SUM(cnt), nick FROM
(SELECT count(*) cnt, e.nick FROM entries e
LEFT JOIN magnets m ON (e.id=m.eid AND e.nick=m.nick)
WHERE eid IS NULL GROUP BY e.nick
UNION ALL
SELECT count(*) cnt, nick FROM magnets m GROUP BY nick) u
GROUP BY nick

Writing a JOIN query with two counts from joined table

I've spent a few hours fighting with this, but I can't get the counts to work. Hopefully someone can help?!
I have a project table and task table, linked on the project_id. I can get the project_id, project_name, and the status_id with the query below:
SELECT
a.project_id,
a.project_name,
b.status_id
FROM project_list as a
INNER JOIN task_list as b
ON a.project_id=b.project_id
I'd like to select a single record for each project and add two count fields based on the status_id. In pseudo code:
SELECT
a.project_id,
a.project_name,
(SELECT COUNT(*) FROM task_list WHERE status_id < 3) as not_completed,
(SELECT COUNT(*) FROM task_list WHERE status_id = 3) as completed
FROM project_list as a
INNER JOIN task_list as b
ON a.project_id=b.project_id
GROUP BY project_id
My create table scripts are below:
CREATE TABLE `project_list` (
`project_id` int(11) NOT NULL AUTO_INCREMENT,
`topic_id` int(11) DEFAULT NULL,
`project_name` varchar(45) DEFAULT NULL,
PRIMARY KEY (`project_id`)
)
CREATE TABLE `task_list` (
`task_id` int(11) NOT NULL AUTO_INCREMENT,
`project_id` int(11) DEFAULT NULL,
`task_name` varchar(45) DEFAULT NULL,
`status_id` int(11) DEFAULT '0',
PRIMARY KEY (`task_id`)
)
Any help is much appreciated. Thanks!
EDIT: ANSWER:
SELECT
a.project_id,
project_name,
SUM(status_id != 3) AS not_completed,
SUM(status_id = 3) AS completed,
SUM(status_id IS NOT NULL) as total
FROM tasks.project_list as a
INNER JOIN tasks.task_list as b
ON a.project_id=b.project_id
GROUP BY a.project_id
The problem is that in your subqueries you are counting all the rows in the whole table rather than just the rows that have the correct project_id. You could fix this by modifying the WHERE clause in each of your subqueries.
(SELECT COUNT(*)
FROM task_list AS c
WHERE c.status_id < 3
AND a.project_id = c.project_id)
However a simpler approach is to use SUM with a boolean condition instead of COUNT to count the rows that match the condition:
SELECT
a.project_id,
a.project_name,
SUM(b.status_id < 3) AS not_completed,
SUM(b.status_id = 3) AS completed,
FROM project_list as a
INNER JOIN task_list as b
ON a.project_id = b.project_id
GROUP BY project_id
This works because TRUE evaluates to 1 and FALSE evaluates to 0.

Mysql many to many problems (leaderborad/scoreboard)

I'm working on a small project in regards of the upcoming World Cup. I'm building a roster/leaderboard/scoredboard based on groups with national teams. The idea is to have information on all upcoming matches within the group or in the knockout phase (scores, time of the match, match stats etc.). Currently I'm stuck with the DB in that I can't come up with a query that would return paired teams in a row. I have these 3 tables:
CREATE TABLE IF NOT EXISTS `wc_team` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(45) NULL ,
`description` VARCHAR(250) NULL ,
`flag` VARCHAR(45) NULL ,
`image` VARCHAR(45) NULL ,
`added` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY (`id`) ,
CREATE TABLE IF NOT EXISTS `wc_match` (
`id` INT NOT NULL AUTO_INCREMENT ,
`score` VARCHAR(6) NULL ,
`date` DATE NULL ,
`time` VARCHAR(45) NULL ,
`added` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY (`id`) ,
CREATE TABLE IF NOT EXISTS `wc_team_has_match` (
`wc_team_id` INT NOT NULL ,
`wc_match_id` INT NOT NULL ,
PRIMARY KEY (`wc_team_id`, `wc_match_id`) ,
I've simplified the tables so we don't go in the wrong direction. Now I've tried al kinds of joins and groupings I could think of, but I never seem to get.
Example query:
SELECT t.wc_team_id, t.wc_match_id, c.id.c.name, d.id, d.name
FROM wc_team_has_match AS t
LEFT JOIN wc_match AS s ON t.wc_match_id = s.id
LEFT JOIN wc_team AS c ON t.wc_team_id = c.id
LEFT JOIN wc_team AS d ON t.wc_team_id = d.id
Which returns:
wc_team_id wc_match_id id name id name
16 5 16 Brazil 16 Brazil
18 5 18 Argentina 18 Argentina
But what I really want is:
wc_team_id wc_match_id id name id name
16 5 16 Brazil 18 Argentina
Keep in mind that a group has more matches I want to see all those matches not only one.
Any pointer or suggestion would be extremely appreciated since I'm stuck like a duck on this one :).
Since a soccer match is made up of always two teams, never more and never less, and the order is also of significance since there is a team 1 and a team 2, I would simply add team_1 and team_2 fields in wc_match and remove the wc_team_has_match table.
This would simplify your query considerably:
SELECT m.wc_match_id,
t1.id AS Team_1_ID,
t1.name AS Team_1,
t2.id AS Team_2_ID,
t2.name AS Team_2
FROM wc_match AS m
JOIN wc_team t1 ON (t1.id = m.team_1)
JOIN wc_team t2 ON (t2.id = m.team_2);
EDIT: Just noticed that you also intend to keep information on matches in the knockout stages, where the teams for each match might not have been decided yet. You can still keep with the same model, making team_1 and team_2 nullable. In that case you would want to use LEFT JOINs instead of INNER JOINs in order to receive a resultset that includes NULL teams. The reason for NULL in SQL is to define unknown information, and therefore fits this case perfectly.
i suggest to change your team_has_match table to teams_for_match which holds a reference for each team:
CREATE TABLE `teams_for_match` (
`match_id` INT,
`team1_id` INT,
`team2_id` INT,
PRIMARY KEY (`match_id`, `team1_id`, `team2_id`)
);
you can then select both teams for a match with:
SELECT *
FROM `teams_for_match` tm
LEFT JOIN `wc_match` m
ON tm.`match_id` = m.id
LEFT JOIN `wc_team` t1
ON tm.`team1_id` = t1.id
LEFT JOIN `wc_team` t2
ON tm.`team2_id` = t2.id;
i hope this works as you expect it to.
ps. apart from that, you could simply add team1_id and team2_id columns to your wc_team table, no need to create an additional table
CREATE TABLE IF NOT EXISTS `wc_match` (
`id` INT NOT NULL AUTO_INCREMENT ,
`score` VARCHAR(6) NULL ,
`date` DATE NULL ,
`time` VARCHAR(45) NULL ,
`added` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ,
`team1` INT FOREIGN KEY REFERENCES `wc_team`(`id`),
`team2` INT FOREIGN KEY REFERENCES `wc_team`(`id`),
PRIMARY KEY (`id`)
)