Updating a column with a Count of data from another column in the same table? [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
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

Related

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)

Joining on BETWEEN values [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
How do I do lookup or joining two tables based on the values of date?
In the first table I have Item_ID and Entry_Date as columns.
On the second one I have Shift_ID, Personnel, Begin_Date, and End_Date as columns.
I want to create a result that displays Item_ID and Personnel, where Personnel and Shift is determined by whether or not an Item's Entry_Date is between a shift's Begin_Date and End_Date.
I'm sorry for utilizing image, I meant to write the table within this post itself but I don't know how yet.
Try a basic join:
SELECT
i.Item_ID,
COALESCE(s.Personnel, 'NA') AS Personnel
FROM Item i
LEFT JOIN Shift s
ON i.Entry_Date BETWEEN s.Begin_Date AND s.End_Date;

Write query better - as it is only working for only 1 row in notice date WITH CURDATE? [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
CREATE TRIGGER NOTICE_ISSUE
AFTER INSERT ON MEMBERS
FOR EACH ROW
INSERT INTO NOTICE (Ssn,LSSN,NOTICE_TYPE) VALUES
((SELECT Ssn from MEMBERS WHERE CURDATE() = NOTICE_DATE),
(SELECT LIB_SSN from MEMBERS WHERE CURDATE() = NOTICE_DATE),'RENEW YOUR MEMBERSHIP');
You are duplicating the select query, it would be better like this:
CREATE TRIGGER NOTICE_ISSUE AFTER INSERT
ON MEMBERS
FOR EACH ROW
INSERT INTO NOTICE (Ssn,LSSN,NOTICE_TYPE)
SELECT Ssn, LIB_SSN, 'RENEW YOUR MEMBERSHIP'
FROM MEMBERS
WHERE CURDATE() = NOTICE_DATE

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

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