I have looked up some questions relating to this topic, but can't seem to find what I need:
I have 2 tables, and I would like to append information from the main table to the other matching one criteria from both. so in one table I have zip code information, I want to append all the users from the main table which match the zip codes in the other table. so i want something like:
any kind of pointers would be greatly appreciated. Thank you!
This should be a simple JOIN of the two tables on the zip_code field. Something like:
SELECT z.zip_code, u.user_id
FROM MyZipCodeTable z
LEFT JOIN MyUserTable u ON z.zip_code = u.zip_code
This will return all zip codes and any matching users.
I think you are looking for all the users from the main table with zip codes in the other table. I assume you can join both tables on user_id.
To get that you want something like this:
SELECT main_table.*, other_table.zipcode
FROM main_table mt
LEFT JOIN other_table ot ON mt.user_id=ot.user_id
Related
I am building a query and I need to select from the log table multiple columns, my issue is that i'm trying to find a way to select a column that has an FK in a table that has an FK to another table.
I have:
log.number_id,
numbers.number_id
numbers.country_id,
countries.country_id
Query is almost done, my only issue is that I need to show countries.country_id through an intermediary table FK numbers.country_id, I believe this is an INNER JOIN yet I have no idea how to create the concatenation, I searched google for this, yet I couldn't find something like a general formula of how to execute such an intermediary join.
I'm guessing you're looking for something like this.
Basically joining the table with both id's to the other tables on the common id.
SELECT log.*, ctry.*
FROM numbers AS ctrylog
JOIN log
ON log.number_id = ctrylog.number_id
JOIN countries AS ctry
ON ctry.country_id = ctrylog.country_id
I have looked through many previous questions but can't seem to find one that matches my query?
My current site has customer details stored across different tables within the database.
I have managed to join the tables based on Firstname and Lastname with:
Select *
FROM ps_customer
INNER JOIN ps_address
ON ps_address.Firstname = ps_customer.Firstname AND
ps_address.Lastname = ps_customer.Lastname
However, the results contain duplicated email addresses for the same customers. What I wanted was to do was return all columns from the joined tables but only for distinct/unique email address? is there a command that can do this?
ps I'm very new to sql, so apologies for nontechnical language
I wanted to do something like in this picture enter image description here
Use DISTINCT:
SELECT DISTINCT
pc.ID,
pc.Firstname,
pc.Lastname,
pa.Address1,
pa.Address2,
pa.Zip,
pc.email
FROM ps_customer pc
INNER JOIN ps_address pa
ON pa.Firstname = pc.Firstname AND
pa.Lastname = pc.Lastname;
I don't know exactly which columns you want to select, but the above pattern should be one way to go here.
One of my tables has a column named "t_name" which supplies an exact name (ie: Google)
And another table has two columns named
m_team_home and m_team_away
Both m_team_home and m_team_away would be INT's in the database but would grab the name from the first table. My joined query is only able to grab the home's team name, and I don't know how to get the away team's name because it will output the same thing.
I know it may be hard to explain, but much help would be appreciated.
sounds like you want to join on the table twice
SELECT a.team, a1.team
FROM table t
JOIN another_table a on a.m_team_home = t.id -- t.id or whatever is in that table that maps to the home / away teams
JOIN another_table a1 on a1.m_team_away = t.id
that way you can get the name for the home team and away team.. you may want to consider making those LEFT joins just incase one doesn't exist and it gets filtered out
I have a single table that contain columns:
UserID, EmployeeID, BadgeType, HiredDate, TermDate
Now I need to find userID that are with (gbro, qunro, 1utny, ybeiot, 4ybey)
The 3 users (gbro, qunro, 1utny) exist so it is listed with respective its column info.
What if ybeiot, 4ybey does not exist AT ALL but still I want them listed in a separate table still but with a message that PRINTS: User that does not exist: ybeiot;4ybey
Help, been finding way how to do this.
I tried JOIN (all the joins) but it does not result to what I wanted.
Did you look at SQL EXISTS keyword?
put all the users to be searched in a temp table or table variable #userstoSearch
select * from #userstoSearch us left join users u
on us.UserID=u.UserID where u.userID is not null
select us.UserID from #userstoSearch us left join users u
on us.UserID=u.UserID where u.userID is null
for xml path('')
You need two selects. The first will list the existing values and the second lists the not existing values. You should merge these results using the union keyword.
There are 4 sql tables:
Listings(Amount, GroupKey, Key, MemberKey),
Loans(Amount, GroupKey, Key, ListingKey),
Members(City, GroupKey, Key)
Groups(GroupRank, Key, MemberKey)
Now, if one wants to find out the loans which are also listings and find the members city and GroupRank for the members in the loan table. Here, the group table contains information about grous of which members are a part of.
and also perform a select operation as given below:
select Listings.Amount, Members.City, Groups.GroupRank
from listings, loans, members, groups
where Listings.Key=Loans.ListingKey and
Members.Key=Listings.MemberKey and
Listings.GroupKey=Groups.Key
The above join is giving an incorrect result, please point out where I am going wrong.
Also I am new to SQL so please excuse the novice question.
Note: The following is just a guess what your problem is. Like others said, clearify your question.
You want to JOIN
( http://dev.mysql.com/doc/refman/5.1/de/join.html )
those tables. What you write is just another form of a join, meaning it has the same effect. But you "joined" a bit too much. To make things clearer a syntax has been invented to make things clearer and avoid such mistakes. Read more about it in the link given above.
What you want to achieve can be done like this:
SELECT
Listings.Amount, Members.City, Groups.GroupRank
FROM
Listings
INNER JOIN Groups ON Listings.GroupKey=Groups.Key
INNER JOIN Members ON Members.Key=Listings.MemberKey
You don't do a SELECT on the Loans table, you don't need it in this query.
This is the INNER JOIN which will give you a result where every row in table A has an according entry in table B. When this is not the case, you have to use the LEFT or RIGHT JOIN.
Maybe the problem is related to the join type (INNER). Try LEFT JOIN for example but Mark has right: you should clearify your question.
I would firstly change your query to use the more modern join syntax, which allows outer joins. Tr this:
select Listings.Amount, Members.City, Groups.GroupRank
from listings
left join loans on Listings.Key=Loans.ListingKey
left join members on Members.Key=Listings.MemberKey
left join groups on Listings.GroupKey=Groups.Key
and/or Loans.GroupKey=Groups.Key
and/or Members.Key=Groups.MemberKey
You may need to play with the criteria on the last join (maybe they should be "or" not "and" etc).