SQL SELECT from 3 tables - mysql

ft_pupils
- id //Primary Key
- name
- start_date
ft_entries
- id
- pupil_id //Foreign Key
- aol_id
ft_aol
- id
- title
For every entry in the ft_entries table I want to use a SELECT to select every entry in the ft_entries table but with the aol_id replaced with the title inside of ft_aol.
I have managed to get:
SELECT name, aol_id FROM ft_pupils, ft_entries WHERE pupil_id = ft_pupils.id
to work fine.
I want the WHERE to be WHERE pupil_id = ft_pupils.id
I am so confused right now.

You can try this to get what you want by join 3 table.
SELECT Name,title FROM ft_entries
INNER JOIN ft_pupils ON pupil_id = ft_pupils.id
LEFT JOIN ft_aol ON ft_aol.id = aol_id

This will show you how to get the data you're looking for and display all the ids. You can remove the ids if you don't care to see them:
SELECT a.id,
b.pupil_id,
c.id,
a.NAME,
a.start_date,
c.title
FROM ft_pupils a
JOIN ft_entries b ON a.id = b.pupil_id
JOIN ft_aol c ON c.id = b.aol_id

Related

How do I get 5 values from 3 different tables? [MYSQL]

MYSQL
I am trying to get 5 values from 3 separate tables where each value is in it's own column.
The values I am trying to get are: SC_Name, SC_Description, AR_Name, AR_Description, AR_id
I have 3 tables where I am pulling the data from:
Service_Category
Area_Responsibility
Key_Scar
Service_Category contains the following columns:
id
SC_Name
SC_Description
Area_Responsibility contains the following columns:
id
AR_Name
AR_Description
Key_SCAR contains the following columns:
id
KS_id
SC_id
AR_id
The intention of having 3 separate tables is so that I can separate the data better in the first two tables and then in the third table link them together on the basis of KS_id and the id from Service_Category and the id from Area_Responsibility.
At the moment I have the following individual queries (KS_id is always 1):
SELECT a.SC_Name, a.SC_Description
FROM Service_Category AS a
where a.id IN (SELECT SC_id
FROM Key_SCAR
WHERE KS_id = 1);
SELECT a.AR_Name, a.AR_Description
FROM Area_Responsibility AS a
where a.id IN (SELECT AR_id
FROM Key_SCAR
WHERE KS_id = 1);
I would like to join these into one query that outputs: SC_Name, SC_Description, AR_Name, AR_Description and AR_id.
I have tried to union these two queries as follows:
SELECT a.SC_Name, a.SC_Description
FROM Service_Category AS a
where a.id IN (SELECT SC_id
FROM Key_SCAR
WHERE KS_id = 1)
UNION
SELECT b.AR_Name, b.AR_Description
FROM Area_Responsibility AS b
where a.id IN (SELECT AR_id
FROM Key_SCAR
WHERE KS_id = 1);
This however does not work for my output as the interpreter I'm using places AR_Name & AR_Description under the column headings SC_Name and SC_Description. I need each of the outputted values to be in their own columns.
I hope this explains it well enough, and thank you in advance for the assistance!
Here's how you can try to achieve your required results by using the join method,
SELECT SC.SC_Name, SC.SC_Description, AR.AR_Name, AR.AR_Description, KS.AR_id
FROM Key_SCAR KS
INNER JOIN Service_Category SC ON SC.id = KS.SC_id
INNER JOIN Area_Responsibility AR ON AR.id = KS.AR_id
WHERE KS.KS_id = 1;

better way of doing this SELECT in MySQL

Best way to do this SELECT?
I've got this tables:
t_department
id
name
t_users
id
name
type
*type can be:
1 SuperUser
2 normalUser
t_department_superuser
(A department can have many superUsers)
-idSuperUser
-idDepartment
t_superuser_normaluser
(A superUser can have many normalusers)
-idSuperUser
-idNormalUser
and finally
t_actions
-id (autonumeric)
-idUser (this can be an id of superUser or normalUser)
-action
Given a department name, for example "mainDepartment"
I need to get all records from t_actions of all normalusers and all superusers of that department
I have this, it works, but I am not an SQL expert (I am using MySQL) and I think it is not the best way to do the select, and t_actions is going to have loads of rows:
SELECT id,idUser,action
FROM t_actions
WHERE (idUser IN (
SELECT DISTINCT t_department_superuser.idSuperUser FROM t_department
RIGHT JOIN t_department_superuser ON t_department_superuser.idDepartment = t_department.id
LEFT JOIN t_superuser_normaluser ON t_superuser_normaluser.idSuperUser = t_department_superuser.idSuperUser
WHERE name='mainDepartment'
UNION ALL
SELECT DISTINCT t_superuser_normaluser.idNormalUser
FROM t_department
RIGHT JOIN t_department_superuser ON t_department_superuser.idDepartment = t_department.id
LEFT JOIN t_superuser_normaluser ON t_superuser_normaluser.idSuperUser = t_department_superuser.idSuperUser
WHERE name='mainDepartment')
ORDER BY id;
Any suggestions to make this better? thank you!!
because you are using left and right joins there will be null records, which is why you need the UNION... you can cut out the UNION with a simple null check
SELECT id, idUser, action
FROM t_actions
WHERE idUser IN
( SELECT DISTINCT COALESCE(tsn.idNormalUser, tds.idSuperUser)
FROM t_department td
RIGHT JOIN t_department_superuser tds ON tds.idDepartment = td.id
LEFT JOIN t_superuser_normaluser tsn ON tsn.idSuperUser = tds.idSuperUser
WHERE td.name='mainDepartment'
)
ORDER BY id;
note i also added alias's to your table names so its easer to write out and read the columns you are trying to select and join on.
EDIT
with the data the only possible way to do it with this table design is like this
SELECT id, idUser, action
FROM t_actions
WHERE idUser IN
((SELECT tds.idSuperUser
FROM t_department td
JOIN t_department_superusers tds ON tds.idDepartment = td.id
WHERE td.name='MAIN')
UNION
(SELECT tsn.idNormalUser
FROM t_department td
JOIN t_department_superusers tds ON tds.idDepartment = td.id
JOIN t_superuser_normaluser tsn ON tsn.idSuperUser = tds.idSuperUser
WHERE td.name='MAIN')
)
ORDER BY id;

What is wrong with this MySQL query (formatting left join)?

I have a query as follows:
SELECT
staff_names.staff_ID AS sid
staff_names.name AS name,
staff_names.rec_type AS rec_type,
prod_staff.specialized AS specialized,
compspec.name AS compspec_name
FROM staff_names JOIN prod_staff USING (staff_ID)
LEFT JOIN (prod_staff_compspec JOIN company_list USING (comp_ID)) compspec
USING (prod_ID, staff_ID, role_ID)
WHERE prod_staff.role_ID = 2
AND prod_staff.prod_ID = 27
AND prod_staff.asst = 'n'
AND episode IS NOT NULL
ORDER BY name
Running this as-is says there's an error near the 'compspec' alias. Removing that and changing 'compspec' to 'company_list' in the SELECT clause returns no rows, even though it should return 1 with the given values. The left join seems to be the problem, but I don't how it should be formatted.
The prod_staff table has prod_ID, staff_ID and role_ID fields. prod_staff_compspec has these and a comp_ID field. prod_staff may or may not have a matching prod_staff_compspec row, but prod_staff_compspec always has a matching company_list row.
What I want to do is retrieve a list of all staff names associated with a given role_ID and prod_ID in the prod_staff table, as well as a company name from the company_list table, if a link to such exists in the prod_staff_compspec table (only a small minority have one).
Switched to ON to define the table relations. LEFT JOIN (prod_staff_compspec JOIN company_list USING (comp_ID)) compspec is switched to 2 left join.
select a.staff_id sid, a.name, a.rec_type, b.specialized, d.name compspec_name
from staff_names a
join prod_staff b on a.staff_id = b.staff_id
left join prod_staff_compspec c on b.prod_id = c.prod_id and b.staff_id = c.staff_id and b.role_id = c.role_id
left join company_list d on c.comp_id = d.comp_id
where b.role_id = 2 and b.prod_id = 27 and b.asst = 'n' and episode is not null
order by a.name;

How to get source and destination city name using inner query in mysql

table_cities - city_id, city_name
table_booking - booking_id, source_city_id, destination_city_id
I want to get Booking_id | source_city_name | destination_city_name as result.
I am trying this:
SELECT * FROM table_bookings
INNER JOIN table_cities
ON table_bookings.source_city_id = table_cities.city_id
INNER JOIN table_cities
ON table_bookings.destination_city_id = table_cities.city_id;
But its giving Not unique table/alias: 'table_cities'
can someone help?
You need to add aliases to your query, this is because you join on the table table_bookings twice and select * the table names are ambiguous, so you need to add aliases after your joins to make it clear:
SELECT
table_bookings.Booking_id,
sourceCities.source_city_name,
destinationCities.destination_city_name
FROM table_bookings
INNER JOIN table_cities AS sourceCities
ON table_bookings.source_city_id = table_cities.city_id
INNER JOIN table_cities AS destinationCities
ON table_bookings.destination_city_id = table_cities.city_id;

JOIN query not behaving as expected

I'm working on a query that doesn't behave as expected, and would appreciate any help on pointing me in the right direction.
TABLES
I have three central tables consists of the following.
table_categories
- id (int)
- name (varchar)
table_events
- id (int)
- name (varchar)
- created (date time)
table_taxonomy
- cat_id (id from table_categories)
- event_id (id from table_events)
- type (varchar in this example always 'category')
GOAL
Count events created after a certain date in each category. The result should be something like:
COUNT NAME ID
3 Rock 1
2 Punk 3
QUERY
This is the query that I've come up with. The problem is that the result doesn't care about the created date, and grabs all events regardles of when they where created.
SELECT COUNT(*) AS count,
            table_categories.name,
            table_categories.id
            FROM table_taxonomy
            INNER JOIN table_categories
            ON table_categories.id = table_taxonomy.cat_id
            LEFT JOIN table_events
            ON table_events.id = table_taxonomy.events_id
            WHERE table_taxonomy.type = 'category'
            AND table_taxonomy.cat_id IN(2,3)
            AND table_events.created > '2012-10-07 05:30:00'
            GROUP BY (table_categories.name)
try this one, just small change while comparing date :
SELECT COUNT(*) AS count,
table_categories.name,
table_categories.id
FROM table_taxonomy
INNER JOIN table_categories
ON table_categories.id = table_taxonomy.cat_id
LEFT JOIN table_events
ON table_events.id = table_taxonomy.events_id
WHERE table_taxonomy.type = 'category'
AND table_taxonomy.cat_id IN(2,3)
AND Date(table_events.created) > '2012-10-07 05:30:00'
GROUP BY (table_categories.name)
Try this one,
SELECT c.ID, c.Name, COUNT(*)
FROM table_categories a
INNER JOIN table_taxonomy b
ON a.id = b.cat_id
INNER JOIN table_events c
ON b.event_ID = c.ID
WHERE c.created > 'dateHere'
-- AND ..... -- other conditions here
GROUP BY c.ID, c.Name