How to create this view using data from other tables - mysql

I have a mysql database with the following tables.
Excursion Table
Excursion ID | Port ID | Excursion Name
Bookings Table
Booking ID | User ID | Excursion ID | Number of People | Event Date
I want to be able to create a view, like the diagram below when given a certain UserID from the bookings table.
Booking ID | Excursion Name | Excursion ID | Number of People

Simply try this:
CREATE VIEW Your_View_Name AS
SELECT BT.Booking_ID, ET.Excursion_Name, ET.Excursion_ID,BT.Number_of_People
FROM Excursion_Table ET
JOIN Bookings_Table BT ON ET.Excursion_ID = BT.Excursion_ID
WHERE BT.User ID = ????;

CREATE VIEW `yourDB`.`yourViewName` AS
SELECT
`a`.`BookingID` AS `BookingID`,
`b`.`ExcursionName` AS `ExcursionName`,
`b`.`ExcursionID` AS `ExcursionID`,
`a`.`NumberOfPeople` AS `NumberOfPeople`
FROM
(`yourDB`.`BookingsTable` `a`
JOIN `yourDB`.`ExcursionsTable` `b`)
WHERE
(`a`.`UserID` = 'YourValue')
Hope this solves your issue.

Related

How to get a starting and ending reservation date for a group in mysql database?

I have a database called global and in that database I have groups table and reservations table, and the relation is one to many (one group can have many reservations).
What I wanted to do was to display the group name along with the first check in date and the last check out date of the reservations.
I am developing using codeigniter 3.x.
Assuming you have this structure of tables:
GROUPS
+--------+-----------+
| id | Name |
+--------+-----------+
and:
RESERVATIONS
+--------+-----------+------------------------------+------------------------------+
| id | Groupid | ReservationCheckinDate | ReservationCheckoutDate |
+--------+-----------+------------------------------+------------------------------+
the SQL to get the desired result would be:
SELECT MIN(ReservationCheckinDate), MAX(ReservationCheckoutDate), Name FROM RESERVATIONS
JOIN GROUPS ON GROUPS.id = RESERVATIONS.Groupid
GROUP BY Name
ORDER BY Name;
hope it helps

mySQL table with EMP_ID and lots of timestamps

I'm new to relation databases and mySQL, I am trying to develop a database for employees, that logs all the times its employees access the system(shown by recording the timestamp of everytime it access the system).
So when the employee accesses the system, the current timeStamp is recorded, and the next time they acess it that current timestamp is also recorded. The idea is that i can go back and query how many times in a day an employee accessed the system or week and so on, for any employee.
so far i have:
EMP_ID | F_Name | L_Name | TimeStamp
-------------------------------------
1222 | joe | blogs | 12.03.22
1222 | joe | blogs | 12.44.34
1352 | carl | mansy | 19.33.22
and so on, i would like to know if there is a way to have just one emp_id show up with all the timestamps below, or do i need another table? or can i just have the data base like this?
Obviously this will grow in size a lot, so would it be better to have a table for every emp_id?
Thanks in advance Jonny
You should have 2 tables
first one is the employee table
emplyee : EMP_ID | F_Name | L_Name
the second one is the log table
employee_log : EMP_ID | TimeStamp
the first table will store the data of the empolyee
the second will store just the log of this employee
and if you want to retrieve the logs you just need to join betwen these tables
select * from employee
left join employee_log on employee.EMP_ID = employee_log.EMP_ID
You should have 2 tables one to store employee data and the other to store
the log data
employee : EMP_ID | F_Name | L_Name
employee_log : EMP_ID | TimeStamp
Assuming all data is stored in table named my_log_table, To see all timestamps for an EMP_ID 12222, query it like
select TimeStamp from my_log_table where EMP_ID = 12222
Having all logs in same table would be scalable and easier to use.
Issues with having multiple log tables:
For each new user, you have to manually create a new table
Privileges have to be granted to this script/user to query the new
table created for a new user
If EMP_ID changes, then you have to track and change table names
Moreover, you are not saving any space other than one column of EMP_ID of
combined log table
I hope it would be better if you maintain your table as below.
EMP_ID | TimeStamp
1222 | 12.03.22
1222 | 12.44.34
1352 | 19.33.22
So, while retrieving you can display as cross table like below
1222 | 1352 |
12.03.22 | 19.33.22
12.44.34 |

mysql query for multiple tables

I have 3 three tables as follows a user can have many contacts and a contact can be listed by many users...
//user table
user_id | username|password|fname|lname|email|contactnumber
//contact table
contact_id | fname | lname |email|contactnumber
//user_contact table
user_id |contact id | relationship type |relationship state
My query must display all the contacts that is link to the selected user ...any advise will be helpful
so it will look like this
Result:
user fname | user lname | email address | contact number of user | contact first name | contact last name | relationship type | relationship state
Correct me if I understand your question wrong:
so the user table and contact table has a many to many relation?
then you can do
select u.fname,u.lname,u.email,u.contactnumber,c.fname,c.lname,uc.relationship_type,uc.relationship_state
from user as u
inner join user_contact as uc on u.user_id=uc.user_id
inner join contact as c on uc.contact_id=c.contact_id
where u.user_id=<userId>

MySQL Query with reference parameter from another table

I apologise in advance if this might seem simple as my assignment needs to be passed in 3 hours time and I don't have enough time to do some further research as I have another pending assignment to be submitted tonight. I only know the basic MYSQL commands and not these types. Please help.
Say that I have two tables:
________________ _________________
| customers | | agents |
|________________| |_________________|
|(pk)customer id | |(pk) agent_id |
|(fk) agent_id | | first_name |
| first_name | | last_name |
| last_name | | address |
|________________| |_________________|
Basically I would just like to know how to query something like: (in incorrect terms)
SELECT * FROM customers WHERE agent_id = '(agent_id of Michael Smith from the AGENTS table)'
obviously I only have the agent_id of the agent and i can directly call it if i know what the agents name is based on the id like:
SELECT * FROM customers WHERE agent_id = '4'
but how can i query it by submitting the agent first name and last name as parameter?
(first name and last name because agents can have the same names, or even same last names)
Remember your foreign key does not help you building the query, you have to tell the database what you want in the query (however, a foreign key can help data spread across tables more consistent).
You can use a JOIN here.
You can implement it like this:
SELECT *
FROM customers C
INNER JOIN agents A ON C.agent_id = A.agent_id
WHERE A.last_name = 'Smith'
AND A.first_name = 'Michael';
You can do it without a join as well.
select *
from customers
where customers.agent_id in (
select agents.agent_id
from agents
where agents.first_name = 'Michael' and agents.last_name = 'Smith'
);

Table structure for storing communities in MySQL

I have a list of communities. Each community has a list of members. Currently I am storing each community in a row with the member names separated by a comma. This is good for smaller immutable communities. But as the communities are growing big, let us say with 75,000 members, loading of communities is becoming slower. Also partial loading of a community (let us say random 10 members) is also not very elegant. What would be the best table structure for the communities table in this scenario? Usage of multiple tables is also not an issue if there is a reason for doing that.
Use three tables
`community`
| id | name | other_column_1 | other_column_2 ...
`user`
| id | name | other_column_1 | other_column_2 ...
`community_user`
| id (autoincrement) | community_id | user_id |
Then to get user info for all users in a community you do something like this
SELECT cu.id AS entry_id, u.id, u.name FROM `community_user` AS cu
LEFT JOIN `user` AS u
ON cu.user_id = u.id
WHERE cu.community_id = <comminuty id>