Joining on BETWEEN values [closed] - mysql

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;

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

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)

SQL query join or subquery [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
[![table named teams and Matches ][2]][2]I have 2 tables
[2]: https://i.stack.imgur.com/7v4nT.png**strong text**
I need data from these 2 tables in such a ways that a new table is formed with the following data
mid, result, date, team1id, team2id from matches table on the basis of tourid=6 and from table of team i need to show the Tname as team1 and Tname as team2.
Sounds like you need a join:
select matches.*,t1.Tname as team1,t2.Tname as team2 from matches
join team t1 on t1.Teamid=matches.team1id
join team t2 on t2.Teamid=matches.team2id
where tourid=6

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