I am trying to do a query in MS Access (which is like going back in time 20 years) and cannot work out how to do alias tables so that two columns in my base table lookup values from another table expecting to get two different corresponding results from the join.
I have two tables - stations and routes. I want to create a query which results in route id, origin and destination which gives me the station name for both.
stations (station_id, station_name)
1,"Auckland"
2,"Wellington"
3,"Hamilton"
routes (route_id, station_from_id, station_to_id)
1,1,2
2,1,3
3,2,3
4,3,2
5,3,1
6,2,3
result from query should be
1, Auckland, Wellington
2, Auckland, Hamilton
3, Wellington, Hamilton
4, Hamilton, Wellington
5, Hamilton, Auckland
6, Wellington, Hamilton
SELECT a.station_id AS station_from, b.station_id AS station_to
FROM routes
INNER JOIN stations ON stations.station_id = routes.station_from_id AS a
INNER JOIN stations ON stations.station_id = routes.station_to_id AS b
That's all I need it to do, but in access, no idea how to make it work, query wizard produces the weirdest results that I can't work with.
Thanks for your help with this, kind regards, Matt
I quickly threw this together to produce the output you want.
select
r.route_id, o.station_name as Origin, d.station_name as Destination
from
((routes r
inner join
stations o on o.station_id = r.station_from_id)
inner join
stations d on d.station_id = r.station_to_id)
The results:
route_id Origin Destination
-------- ------------ ------------------
1 Auckland Wellington
2 Auckland Hamilton
3 Wellington Hamilton
4 Hamilton Wellington
5 Hamilton Auckland
6 Wellington Hamilton
Related
I have a list table having over 200,000 rows with city column. Assume that it contains following data:
rowid city
1 Toronto
2 Milton
3 Hamilton
4 Delhi
5 New Delhi
6 Markham
I want to find all records where the city is contained in another row, e.g. Milton (row 2) is contained in Hamilton (row 3) and Delhi (row 4) is contained in New Delhi (row 5). I expect the following output:
rowid city rowid2 city2
2 Milton 3 Hamilton
4 Delhi 5 New Delhi
Can a single query get the output I am looking for?
Thanks.
Assume table name is 'cities'. Following SQL query should work:
select
c2.rowid, c2.city, c1.rowid, c1.city
from
cities c1
inner join
cities c2
on instr(c1.city,c2.city)
and c1.rowid != c2.rowid
Folks can you please give your suggestions for my question regarding mysql joins.
My Table structures:
place table:
place_id place_name city
1 Hotel Golconda Hyderabad
2 Paradise Hotel Hyderabad
3 Hotel Mayuri Hyderabad
place_tags
tag_id tag_name
1 Valet Parking
2 Air Conditioned
3 Buffet
4 Bar
5 Family Dining
places_info Table:
place_id tag_id
1 1
1 2
1 3
2 1
2 5
3 1
3 4
The above is all my tables which are containing the place names and address in places table, all the facilities of the restaurants in tags table and mapping of the facilities of each place in places_info table.
Is this my table structures are correct to get the places which had "Valet parking and Buffet". How can write a join query for this type of results to get.
Most Importantly we had millions of places in places table and also in the places_info table. How to achieve maximum performance with this type of table structure? Or shall I need to change the table structures?
Please guide me.
This'd be the basic structure for "places with valet AND buffet":
SELECT place_id, COUNT(places_info) AS cnt
FROM place
LEFT JOIN places_info ON place.place_id = places_info.place_ID
AND tag_id IN (1, 3)
^^^^---- two tags: valet(1) + buffet(3)
GROUP BY place.place_id
HAVING cnt = 2
^^^---- must have both tags
For a places which have NEITHER of the tags, or only one, the count would come back 0, or 1, and get dumped by the HAVING clause.
I am really stuck on how to create the appropriate select statement and would appreciate any guidance you can offer.
I have created a mini document management system that allows users to upload files and to categorize those files by categories. Each file can have one or many categories selected. Following are my tables:
Table: files (pKey (primary key), file_name, file_description, file_path)
pKey file_name file_description file_path
1 IT001.DOC Network Design Document /common/it/
2 IT002.DOC Desktop Standards /common/it/
3 IT003.DOC Laptop Standards /common/it/
There are other departments in addition from IT so the path field also changes (Just thought I would toss that bit of datum in)
Table: categories (pKey (primary key), category_description)
pKey category_description
1 Central Missouri Campus
2 Eastern Missouri Campus
3 Western Missouri Campus
4 Desktops
5 Laptops
6 Networks
7 Printers
Of course there are other categories as well, this is just a sampling
Table: category_xref (pKey (primary key), fk_file_id, fk_category_id)
pKey fk_file_id fk_category_id
1 1 1
2 1 2
3 1 6
4 2 2
5 2 3
6 2 4
7 3 1
8 3 2
9 3 3
10 3 5
When the user searches for related documents they are presented a form with the Category checkboxes. By choosing Central, they get all files that have been marked as Central. By choosing Desktops, they get any documents that have been marked as Desktops. However, when they select Central AND Desktops they get any document that is either Central OR Desktops. I need to figure out how to get only those documents that are BOTH Central AND Desktops AND any other checkboxes they have selected, and exclude any that do not contain ALL the checkboxes selected.
SELECT f.pkID, f.file_name, f.file_description, f.file_path, cox.fk_category_id
FROM files f
JOIN category_xref cox ON cox.fk_file_id = f.pkID
WHERE cox.fk_category_id IN (59, 69)
ORDER BY f.file_name ASC, cox.fk_category_id ASC
The simplest way would probably be:
SELECT f.pkID, f.file_name, f.file_description, f.file_path
FROM files f
JOIN category_xref cox ON cox.fk_file_id = f.pkID
WHERE cox.fk_category_id IN (59, 69)
GROUP BY f.pkID
HAVING count(distinct cox.fk_category_id)=2
ORDER BY f.file_name ASC
You can try something in these lines:
SELECT f.pkID, f.file_name, f.file_description, f.file_path
FROM files f
RIGHT JOIN category_xref cox1 ON cox.fk_file_id = f.pkID AND cox1.fk_category_id = 59
RIGHT JOIN category_xref cox2 ON cox.fk_file_id = f.pkID AND cox2.fk_category_id = 69
ORDER BY f.file_name ASC
I'm trying to retrieve data from tables and combine multiple rows into a single column, without repeating any information.
I have the following tables: profile, qualification, projects.
Profile
pro_id surname firstname
------ ------- ----------
1 John James
2 King Fred
3 Luxury-Yachts Raymond
Qualification
pro_id Degree School Year
------ ------ ------ -----
1 MBA Wharton university 2002
1 LLB Yale University 2001
2 BSc Covington University 1998
2 BEd Kellog University 1995
Projects
pro_id Title Year
------ ------ ------
1 Social Networking 2003
1 Excavation of aquatic debris 2007
2 Design of solar radios 1992
2 Development of expert systems 2011
I want to retrieve the all of the information for each person, with each person appearing only once in the result. The info on qualifications and projects should each be in their own column (one column for qualifications, another for projects), separated by commas. For example, the results for the above sample data should be:
1 John James MBA Wharton university 2002, LLB Yale University 2001 Social Networking 2003, Excavation of aquatic debris 2007, Design of Solar panels 2008
2 King Fred BSc Covington University 1998, BEd Kellog University 1995, Msc MIT 2011 Design of solar radios 1992, Development of expert systems 2011
3 Raymond Luxury-Yachts
Currently, I have the query:
SELECT pro_id,
surname,
firstname,
group_concat(degree,school,year) AS qual,
concat(Title,year) AS work
FROM profile,
LEFT JOIN qualification
ON qualification.pro_id = profile.pro_id
JOIN projects
ON projects.pro_id = profile.pro_id
GROUP BY pro_id
For the sample data, this query results in:
1 John James MBA Wharton university 2002, Social Networking 2003
1 John James LLB Yale University 2001, Excavation of aquatic debris 2007
1 John James MBA Wharton university 2002, Social Networking 2003, Excavation of aquatic debris 2007
etc
Note: Raymond Luxury-Yachts isn't present in the current result.
I don't want duplicate result records. Also if the surname does not have any entry in the qualification and projects table, I want the query to return the name and display an empty field in the qualification and projects table instead of omitting them altogether.
Replace LEFT JOIN with JOIN
Select pro_id, surname, firstname, group_concat(degree,school,year) as qual,concat(Title,year) as work
from profile
join qualification on qualification.pro_id = profile.pro_id
join projects on projects.pro_id = profile.pro_id group by pro_id
What is the difference between "INNER JOIN" and "OUTER JOIN"?
Using Join will fix the issue with displaying values even if there are no records in the projects table.
For the first question, you can try making a stored function and calling it from the select statement. This function will take pro_id as parameter, create the concatenated string and return it. That's the only solution for MySQL that I can think of at the moment.
I think you are close on your thoughts of group_concat. However, with possible No values (thus leaving nulls), can cause problems. I would have each secondary table pre-concatinated by person's ID and join to THAT result. Eliminates the problem of nulls
SELECT
p.pro_id,
p.surname,
p.firstname,
PreQConcat.UserQual,
PrePJConcat.UserWork
FROM
profile p
LEFT JOIN
( select q.pro_id,
group_concat( q.degree, q.school, q.year) AS UserQual
from
qualification q
group by
q.pro_id ) PreQConcat
ON p.Pro_ID = PreQConcat.pro_id
LEFT JOIN
( select pj.pro_id,
concat(pj.Title, pj.year) AS UserWork
from
projects pj
group by
pj.pro_id ) PrePJConcat
ON p.Pro_ID = PrePJConcat.pro_id
You are going through all people anyhow, and want all their respective elements (when they exist) grouped, so why group on a possibility it doesn't exist. Let the JOINED queries run once each, complete with a single result grouped by only those people it had data for, then join back to the original profile person.
I have a database two tables and a linking table that I need a JOIN query for:
Here are my Tables:
family (userid (int), loginName, etc)
member (memberid (int), loginName(this links member to a family),name, etc)
Linking Table:
user2member (userid,memberid)...would both be foreign keys?
I want to do two things:
1) Be able to have a family.loginName(12,Johnson) subscribe to another family.loginName (43,Smith) and record that into the linking table.
That would look like this:
12,43
2) When I do a query for all the member.name that are in the Johnson Family, I'll get all the Johnsons & all the Smiths.
If Johnson = Ted, Sue & Patty
IF Smith =Joe, Sue & Bob
my query results would be Johnson now = Ted,Sue,Patty,Joe,Sue,Bob
I asked this question a few days ago without good table names and I ended up confusing myself and the nice guy Ollie Jones who posted an answer similar to this for the query:
SELECT member.name
FROM family
JOIN user2member on family.userid = member.memberid
JOIN member on user2member.name = member.name
WHERE family.userid = '30'
ORDER BY member.name
I had to change Ollie's answer to match my tables but I'm getting a limit error 0,30 on line 5.
This is my first time doing JOINS and I have no idea if this is correct.
Thanks,
Here's the link to my first question: mySQL table linking , group linked to other members lists, the displaying all members
I am not sure, if the tables you suggested would solve your problem. If I understand your question correct, there are two relationships:
a relationship for all family members (Johnson with Ted, Sue, Patty, Smith with Joe, Sue, Bob)
a relationship for subscriptions (a family can subscribe to another family)
I would suggest following tables:
family (f_id, f_loginName, etc.)
member (m_id, m_f_id, m_name) (foreign key to family, many-to-one relationship)
subscription (s_f_id,s_to_f_id) (linking is based on both family keys)
This would result in following contents:
family:
f_id f_loginName
12 Johnson
43 Smith
member:
m_id m_f_id m_name
1 12 Ted
2 12 Sue
3 12 Patty
4 43 Joe
5 43 Sue
6 43 Bob
subscription
s_f_id s_to_f_id
12 43
Now, to get all possible members for a specific family and it's subscriptions, I would use following SQL query. It has a simple join for family and it's family members. In the WHERE clause, the family Johnson is fetched (f_id = 12) and to get all family members from the subscriptions, it's easier to use a subquery.
SELECT f_loginName, m_name
FROM family
INNER JOIN member ON m_f_id = f_id
WHERE f_id = 12
OR f_id IN (SELECT s_to_f_id FROM subscription WHERE s_f_id = 12)
ORDER BY f_loginName, m_name;