Getting data from one table if not also in another [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 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)

Related

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

How to fetch data from mysql table and insert into another table using php? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Here is my sql query below
select Client, count(*) as Sales FROM sale_tbl GROUP BY Client
The query will fetch records like this
Client Sales
John 5
Bob 4
Doe 5
Now i want to insert this record into another table in the same database named data_tbl with three(3) columns of: id(Auto increment), Client, Sales
You can just INSERT INTO other_table (Client, Sales) SELECT ... with the SELECT you have here on the end.
That being said, what you want is probably a VIEW instead with CREATE VIEW:
CREATE VIEW client_sales AS
SELECT Client, count(*) AS Sales FROM sale_tbl GROUP BY Client
The advantage of a view is it's always up to date. If you're having performance problems you could look into materialized views that can help address that.

Duplicate value while using Group By [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
SELECT location,special,price FROM `tickets`
WHERE event = 'food' GROUP BY location
I think you want, (assuming its mysql) the following:
SELECT location, ANY_VALUE(special) special, ANY_VALUE(price) price
FROM tickets
WHERE event='food'
GROUP BY location ;
ANY_VALUE is strange aggregate function out of normal db standards that returns one of the values arbitarily.
If you wish to not have duplicate results in your rows you can choose only distinct result set.
SELECT DISTINCT location,special,price FROM tickets WHERE event = 'food' GROUP BY location

Querying a 1 to many table where the many side has at least one row [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 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'
);

MySQL select from two tables and combine results? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a question about MySQL: I want to list results from a table but gather additional information about these results from another table by a foreign key.
My table YTPrograms has a foreign key called Author which corresponds to the Id of a second table called YTUsers from which I want to add the username to my result from the YTPrograms query. I'm not sure how to do this without having to query (the users table) for each result.
Try this:
SELECT p.something, u.username FROM YTPrograms p, YTUsers u WHERE p.Author = u.Id