Querying a 1 to many table where the many side has at least one row [closed] - mysql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Have a common schema, with two tables.
users table contains the columns id, name.
checkins table contains the columns user_id, checkin_date.
in this hypothetical, users can have many instances of rows in the checkins table.
checkin_date in this case is of type date
I want to be able to query for all users who have at minimum 1 checkin in the checkins table where the checkin is after 2016.

I would suggest using exists:
select u.*
from users u
where exists (select 1
from checks c
where c.user_id = u.id and c.checkin_date >= '2016-01-01'
);

Related

sql - mysql - tables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed yesterday.
Improve this question
I need to collect the name and total order of customers who had an order of more than 56000. there are three tables, customers, orders, and orderdetails.
This is the output that I need:
this is the code that im trying:
select a.customername, sum(c.priceeach*c.quantityordered) as "ORDERTOTAL"
from customers a
join orders b on b.customernumber = a.customernumber
join orderdetails c on c.ordernumber = b.ordernumber
having sum(c.priceeach*c.quantityordered) > 58000
group by a.customername;
but it is giving me 76 rows.
when I only try with the table order details i can get exact 7 rows but the table orderdetails doesnt contain customername, I can't undrestand where am I doing it wrong.

Updating a column with a Count of data from another column in the same table? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
As the title suggests, trying to take the count or possibly distinct count of a column, we call it year, to count the number of a years for an individual or populated ID and place it another column in the same table. Here is an idea of what I have so far and we want to update the table. Thank you.
Join group table to updated table directly:
UPDATE outputtable O JOIN
(SELECT personID, COUNT(DISTINCT year) AS countYear
FROM outputtable GROUP BY personID) temp ON O.personID=temp.personID
SET O.N=temp.countYear

Getting data from one table if not also in another [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I've got a database with two relevant tables (times and registrations).
I want to get the times from the times table, through an SQL query, that aren't in the registrations table (don't have a registration on them). Registration at 5PM, wouldn't show 5PM from times in the query.
My current query is:
SELECT time FROM `times` WHERE time IN (SELECT time FROM `registrations` WHERE ID IS NOT NULL)
(registrations DO have an ID)
This does the opposite of what I want it to do (shows all times, regardless of registration, or not).
How could I get the opposite effect?
You seem to want not exists:
select t.*
from times t
where not exists (select 1 from regisrations r where r.time = t.time)

mySQL select every record that does not exist in another table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have 3 tables:
users (id_user)
achievements (id_achievement)
user_achievements (id_user, id_achievement)
I need help selecting every achievement that a user has not yet completed.
Consider a cross join that generates all combinations of users and achievements, and a not exists condition with a correlated subquery to filter out those that were completed already:
select u.id_user, a.id_achievement
from users u
cross join achievements a
where not exists (
select 1
from user_achievements ua
where ua.id_user = u.id_user and ua.id_achievement = a.id_achievement
)

Display group data along with their last latest message [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to display result having data categorized in the group with their latest message according to date.
I tried grouping and having along with nested queries but no luck.
SELECT groupName,date FROM chat where groupName like '%he%' group by groupName,date having min(date);
I want the two rows to be printed along having the latest message with them.Here it would be row 4 and row 5. this is just an arbitrary data though which I need to impplement on bulk.
You don't need group by, just filtering:
select c.*
from chat c
where c.date = (select max(c2.date) from chat c2 where c2.groupname = c.groupname);
You can do it with NOT EXISTS:
SELECT c.*
FROM chat c
WHERE
groupName LIKE '%he%'
AND
NOT EXISTS (
SELECT 1 FROM chat
WHERE groupname = c.groupname AND date > c.date
)