I have 2 tables, table one contains phone numbers and owner name, and table two have phone , name and also its have city and street. the problem in table two is that the owner name is the name of the one who pays for the phone number and not the actual owner (for example if my dad pays for my phone number, his name will appear along side my number). what i want to do is to write a query that checks if the phone number exists on both tables and if it does, i want to see the owner name from table one and the other info from table two.
also if there is a number that exists only on table two i want to see it to.
and if the number is only on table one i want to see it also but with the address and city column empty.
example for expected result:
i have no idea how to do this, it will be very helpful if someone could tell me where to look for answer.
You want a full join. A simple method to implement this with SQLite is to use union all:
select t2.address, t2.city, t1.phone, t1.name
from table1 t1 left join
table2 t2
using (phone)
union all
select t2.address, t2.city, t2.phone, t2.name
from table2 t2
where not exists (select 1 from table1 t1 where t1.phone = t2.phone);
Related
I'm new to programing and especially to SQL and I have one table called team where I keep the different teams and I want to show them on another table called game where it would have the team1 and team2 columns with the different teams.
This is for a school project and is to show the data in a website.
I tried with an INNER JOIN but that didn't work.
I already did something similar with the stadium, but the difference is that one is only one column and this one is with two columns so I don't know if it's possible.
enter image description here
enter image description here
You just need to join with team table twice -
select * -- choose whatever columns you need
from game g
,team t1
,team t2
where g.team_id_1 = t1.id
and g.team_id_2 = t2.id
Hello everybody here is my issue. (TRANSACT-SQL)
I have two tables, Table 1 is my main table, and it has all Student ID's, and the first and last names associated (StudentID, First Name, Last Name)
Table 2 has students with failing scores. The issue is, The first and last names of students are NOT matched with any ID's, as in, the entire studentID column is blank(Full of NULL values, because it is blank). (The first and last names are correct, and are in table 2)
How do I write some sort of a query(or update) to permanently fill this column in with the correct ID's. (Again, the first name, last name, and matching ID columns are available in table 1)
If it is easier I can merge the first and last name column, but I would prefer to leave them that way.
Thank you!
You can use an UPDATE query with a FROM to join your tables. The big caveat here is that this will break if you have more than one person sharing the same name.
UPDATE t2
SET t2.id = t1.id
FROM table2 as t2
INNER JOIN table1 as t1
ON t2.firstname = t1.firstname AND
t2.lastname = t1.lastname
I have two tables.
- Table 1 includes (ID, Name, gender, interestsID)
- Table 2 includes (ID, interests).
Now I want to have a new SELECT statement which show me instead of the interestsID the name behind that ID which is of course in an another table. So basically a mapping must be done and reflect the correct value behind the ID.
You need to join the two tables using I.D. The below code should work assuming your tables are actually called Table1 and Table2.
SELECT Table1.Name
FROM Table1
INNER JOIN Table2
ON Table1.ID =Table2.ID;
In order to understand joins you should refer to this link http://www.w3schools.com/sql/sql_join.asp.
I have two tables, TABLE1 and TABLE2. TABLE1 Has 4 columns: Name, Client, Position, and ID. TABLE2 has 3 columns: Amount, Time, and ID. For every ID in TABLE1 there are one or more entries in TABLE2, all with identical ID and Time values but varying Amount values.
In a view, I have a concatenated string of Name, Client, Position, and ID from TABLE1, but I also need to concatenate the Time for each ID from TABLE2 to this string. If I do a join as I create the view, I get a ton of duplicate strings in the view as it lists the same ID multiple times for each value of Amount in TABLE2.
I need to get rid of the duplicates, so I either need to avoid the replication that occurs from the join, or find a way to simply delete all the duplicates from the view.
Hopefully this is all clear enough. Thank you for reading and for any help you can provide!
DISTINCT could be appropriate:
CREATE VIEW [dbo].[View1]
AS
SELECT distinct
dbo.Table1.Id,
dbo.Table1.Name,
dbo.Table1.Client,
dbo.Table1.Position,
dbo.Table2.[Time]
FROM dbo.Table1
LEFT OUTER JOIN dbo.Table2
ON dbo.Table1.Id = dbo.Table2.Id
SqlFiddle: http://sqlfiddle.com/#!3/65651/1
On a side note, depending on what your table structures really are, you might consider having a surrogate primary key on the table2 if you don't have a natural one. I have not put one in the example, because chances are that you already have one - just making sure.
I have a user table associated to a website with when customers forget there password they create a new account rather then be bothered retrieving their forgotten password.
I would like to see how many times a customer may have joined the website by joining the user table to itself using the customer email address and the ID would be unique each time they joined and I put a statement in checking to see if the Account IDs are different.
Here is my query:
`Select Distinct
T1.Email as "eMail-01", T2.Email as "eMail-02", T1.AccountID as "AccountID-01", T2.AccountID as "AccountID-02", T1.UserID as "UserID-01", T2.UserID as "UserID-02"
From User T1
Left Join Users T2 on T1.eMail = T2.eMail
Where ( T2.eMail is not null ) and ( T2.eMail <> '' )
and ( T1.AccountID <> T2.AccountID )`
The table has about 60,000 records in it and I seem to be getting a great number records returned base on the number of AccountID permeations.
For instance 1 Customer registered 5 times with the same email address, so I’m getting 25 records back (5 x 5). I’m not sure if I’m writing this query correctly.
The query is running very long.
If I understand correctly, what you most probably want is a count of AccountID per email address so there's no need for self join here. The query would be :
SELECT Email, count(AccountID)
FROM User
GROUP BY Email
and should run quite quickly event with 60.000 emails.
Anyway, you should think about putting a UNIQUE index on the email column after cleaning the table. You could then benefit with email search performances and prevent users to create multiple account with the same e-mail address. This should help them retrieve their password instead.
Suggestions:
First, you are using a left join which is useless, because you filter the records that are not null on the right side of the relation (which can be acomplished with a simple inner join). Either you use inner join or remove the condition T2.eMail is not null.
Secondly, is your table properly indexed? If it is not, add the appropriate indexes.
Thirdly, you can use a very simple query to track down the emails that have more than one accountId:
select email, count(accountId) as accounts
from user
group by email
having count(accountId) > 1
Then you can work using only the emails that have more than one account.
Hope this helps