I'm having problems when making a left join with SQLite.
structure of the tables
contacts: id_c, first_name, last_name, photo_link
messages: id_msg, id_sender, id_recipient, utimestamp, message, chatID
I'm doing a query as follows:
SELECT * FROM messages m
LEFT OUTER JOIN contacts c ON (c.id_c=m.id_sender)
WHERE m.chatID=26
This query returns values from the message table.
But returns null for the contacts table.
I tried several ways and can not resolve this problem. Any suggestions to solve this?
The problem was solved. Probably there was a bug. Once tested in SQL Fiddle, I realized that it worked without problems. Thank you for your attention.
Returning nulls for the columns from the contacts table is exactly what I'd expect from a working LEFT JOIN in the case that there isn't a contact whose id_c matches the id_sender for chatID 26. Have you double-checked that you have such a contact in the contacts table?
Related
i tried to figure out how to do the number 7 query on Helpdesk DB on SQLZOO (medium questions) but I can't. Not even using subquery or not exist statement. Here is the link (n. 7).
http://sqlzoo.net/wiki/Helpdesk_Medium_Questions
select first_name, last_name
from Caller a
left join Issue b
on a.Caller_id = b.Caller_id
where Call_date is null
This query says -
select ALL rows from Caller table regardless of them having a join
select ONLY rows from Issue where they have a join
Therefore, any row that returns from the Caller table that has not made a call will have a null value for Call_date. The where clause says only give me those rows.
You need to use LEFT JOIN on the Issue table and then add a filter where a field in the Issue table is null
SELECT ca.First_name, ca.Last_name
FROM Caller ca
LEFT JOIN Issue i ON ca.Caller_id = i.Caller_id
WHERE i.Caller_id is null
So I have a list of people that I got from table 1.
SELECT UA.user FROM UserAcct UA
I have another table that I need to check against to make sure that the user is not listed in table 2.
LEFT OUTER JOIN AccountsLog AL ON AL.user = UA.user
Ultimately what I am trying to do is make sure that people from table 1 have performed an action on table 2, otherwise their name will be returned. Performing this action will cause their name to show up in table 2. Any help here is greatly appreciated. I am happy to try and elaborate further if this isn't enough information. Thanks!
when you do a left outer join to find records in TABLE A that are not in table B, simply look for NULLs.
So when you LEFT OUTER JOIN the tables on ON AL.user = UA.user, you can then find the missing records with the clause:
WHERE AL.user IS NULL
So if I understand correctly you want Anything from User that not exists in AccountsLog, I think you're on the right track
SELECT UA.user FROM UserAcct UA
LEFT OUTER JOIN AccountsLog AL ON UA.user = AL.user
WHER AL.User is null -- this will give you anything that is on user and not in accountslog
Regards
If your version of SQL supports it, try using Not Exists. Subqueries with NOT EXISTS. I've found it is faster than "left outer joins and filtering for null values".
This will give the users only on UA and not on AL
SELECT UA.user FROM UserAcct UA
MINUS
SELECT AL.user FROM AccountsLog AL
Edit:- For SQL server use EXCEPT instead of MINUS (Supported by Oracle)
I'm trying to write a query in which I should get the company information; however, I should be getting only 2 record and I'm getting 6 records.
Below is my query.
SELECT distinct a.FOLIO
,a.MAIN_ADDRESS1
,a.MAIN_ADDRESS2
,b.COMPANY_NAME
,b.FIRST_NAME
,b.LAST_NAME
,a.OPEN
,a.CLOSE
,c.CC
,c.CNAME
FROM vw_CODE_CASE AS a
INNER JOIN vw_CODE_CASE_VIOLATOR_CONTACTS AS b ON b.CMCODECASEID=a.CMCODECASEID
INNER JOIN vw_CODE_CASE_WORK_FLOW AS c ON c.CMCODECASEID=a.CMCODECASEID
Is it possible that is because of the amount of inner joins?
Thank you for your help.
Without sample data it would be difficult to identify that due to which column the distinct data are not coming from your query.
But you can do one thing to identify the cause, You can try remove one by one column and check the data from your query. At the point when you get your expected data, the last removed column will be the cause behind your problem.
Hope this helps.
I have a large table User and a small table User_purchase in google bigquery.
If I join the two with
SELECT User.id, User_purchase.amount FROM User
LEFT JOIN User_purchase on User.id = User_purchase.user_id,
the query returns error:
Query Failed. Error: Not Implemented: This table cannot be read
But if I join the two with
SELECT User.id, ISNULL(INTEGER(User_purchase.amount), INTEGER(0)) FROM User
LEFT JOIN User_purchase on User.id = User_purchase.user_id,
the query works.
Don't quite understand why the first case does not work.
I assume in the first case I can get all users with their purchase_amount though some users will have NULL as their purchase_amount.
Thanks.
This is a bug relating to nested field names in query replies. I've got a fix for the bug but it won't go out until next week's release. Thanks for bringing it to our attention.
I've got a table with 500.000 records filled with Twitter updates. Then I've got a table with user info.
I basically need all the Twitter records of the people in my user table.
I can do it with this SELECT IN query:
SELECT *
FROM STATUS WHERE twitterUserID
IN (
SELECT twitteruserid
FROM accountLink
)
But that's obviously very slow.
I then tried to do it with a join, but it only shows 7 records. No idea why.
SELECT status . * , accountLink.userId, accountLink.twitterUserId
FROM status
JOIN accountLink
ON status.twitterUserId = accountLink.twitterUserId
Does anyone know what could cause this behaviour and how to solve it?
Try changing it to this:
SELECT status.* , accountLink.userId, accountLink.twitterUserId
FROM status
LEFT JOIN accountLink
ON status.twitterUserId = accountLink.twitterUserId
I suspect that there aren't matches for all the records between status and account link. Doing a left join will select every status regardless of whether or not accountLink has a match.
The JOIN syntax should work, unless the column data types are different.
Per the MySQL Documentation for IN():
The search for the item then is done using a binary search. This means IN is very quick if the IN value list consists entirely of constants. Otherwise, type conversion takes place according to the rules described in Section 11.2, “Type Conversion in Expression Evaluation”, but applied to all the arguments.
Ensuring that your column types match should ensure that the JOIN syntax works correctly.
SELECT s.*, a.twitterUserId, a.userId
FROM status AS s INNER JOIN accountLink AS a
WHERE s.twitterUserId=a.twitterUserId
You DO want to use inner join because you only want to return results IF the "status" table has a record AND a corresponding user record is found in the "accountLink" table. If a "status" table record does NOT have a corresponding user entry, you shouldn't display it (at least according to your post). LEFT OUTER JOIN would display status table records even if there was not a matching entry in the accountLink table.
Here's a great resource for learning about SQL joins:
SQL Joins (w3schools.com)