I am joining 2 tables using RIGHT JOIN statement. I used below query and it works good. However it still display all data whenever I tried to select specific user
SELECT TBLNOTIFICATIONS.NOTIFICATION_ID, TBLNOTIFICATIONS.NOTIFICATION_TYPE, FILENAMES_LIST.LOCATION_FILENAME, TBLNOTIFICATIONS.NOTIFICATION_DATE
FROM TBLNOTIFICATIONS
RIGHT JOIN FILENAMES_LIST
ON TBLNOTIFICATIONS.NOTIFICATION_ID=FILENAMES_LIST.NOTIFICATION_ID
WHERE TBLNOTIFICATIONS.USER_ID='JCON'
What should I do to select data from specific user?
Thanks in advance.
You are filtering on the left table, so all the data of the right table will still be shown.
It is probably enough to change the query to a LEFT JOIN to get the results you want.
Besides that, you can use aliases to make your query more readable, like so:
SELECT tn.NOTIFICATION_ID, tn.NOTIFICATION_TYPE, fl.LOCATION_FILENAME, tn.NOTIFICATION_DATE
FROM TBLNOTIFICATIONS AS tn
LEFT JOIN FILENAMES_LIST AS fl
ON tn.NOTIFICATION_ID = fl.NOTIFICATION_ID
WHERE tn.USER_ID='JCON'
Related
I'm new to SQL and im wondering if something like this is possible.
It's probably a super simple solution but i cant seem to solve it. I'm using an Oracle database.
Employee_Table:
employeeNr
username
address
epost
Computer_Table:
employeeNr
ComputerNumber
ComputerUpdated
When I'm using
SELECT *
From Employee_Table,
Computer_Table
WHERE Employee_Table.EmployeeNr = Computer_Table.EmployeeNr
AND Employee_Table.username LIKE ('%SomeUsername%')
When I'm using this sqlstring I only get users with computers. I would like to get all users and the computers of those who has one.
Now the million dollar question. What modifications do I need to make?
Never use commas in the FROM clause. Always use explicit JOIN.
In this case, you want a LEFT JOIN:
SELECT *
FROM Employee_Table e LEFT JOIN
Computer_Table c
ON c.EmployeeNr = c.EmployeeNr
WHERE e.username LIKE '%SomeUsername%';
When you use LEFT JOIN, conditions on the first table go on the WHERE clause. Conditions on the second table go in the ON clause.
I'm trying to get 2 results into 1 html table, the results of a user being kicked/banned. A UUID is a unique code for every user.
The UUID is stored in BAT_players.
The player name is also stored in BAT_players
There are 3 table's: BAT_players, BAT_ban and BAT_kick
I'm trying to get the history of a user in a html table, this includes kicks and bans. Right now there are only bans in this history, i'm trying to add kicks too. This query is working fine, it shows only bans though.
SELECT BAT_ban.ban_staff, BAT_ban.ban_state, BAT_ban.ban_server, BAT_ban.ban_begin, BAT_ban.ban_end, BAT_ban.ban_id, BAT_kick.kick_id, BAT_ban.ban_reason, BAT_players.BAT_player, ban_soort
FROM BAT_players
INNER JOIN BAT_ban
ON BAT_ban.UUID=BAT_players.UUID
Unfortunately it is not working with this query, it's giving me an empty history. What am i doing wrong with the second inner join?
SELECT BAT_ban.ban_staff, BAT_ban.ban_state, BAT_ban.ban_server, BAT_ban.ban_begin, BAT_ban.ban_end, BAT_ban.ban_id, BAT_kick.kick_id, BAT_ban.ban_reason, BAT_players.BAT_player, ban_soort
FROM BAT_players
INNER JOIN BAT_ban
ON BAT_ban.UUID=BAT_players.UUID
INNER JOIN BAT_kick
ON BAT_kick.UUID=BAT_players.UUID ORDER BY ban_id DESC
;
Thanks!
The problem is that a "null" on any of your inner joins will eliminate that row from the result set.
One solution (perhaps the best solution) is to use left joins.
Another is to take the UNION of two inner joins.
Here's a great link to help visualize INNER, OUTER, LEFT and RIGHT joins:
http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins
Try using LEFT JOIN:
SELECT BAT_ban.ban_staff, BAT_ban.ban_state, BAT_ban.ban_server, BAT_ban.ban_begin, BAT_ban.ban_end, BAT_ban.ban_id, BAT_kick.kick_id, BAT_ban.ban_reason, BAT_players.BAT_player, ban_soort
FROM BAT_players LEFT JOIN
BAT_ban ON BAT_ban.UUID=BAT_players.UUID LEFT JOIN
BAT_kick ON BAT_kick.UUID=BAT_players.UUID
ORDER BY ban_id DESC
To replace NULL values with empty strings (for SQL Server):
SELECT ISNULL(BAT_ban.ban_staff,''), ISNULL(BAT_ban.ban_state,''), ISNULL(BAT_ban.ban_server,''), ISNULL(BAT_ban.ban_begin,''), ISNULL(BAT_ban.ban_end,''), ISNULL(BAT_ban.ban_id,''), ISNULL(BAT_kick.kick_id,''), ISNULL(BAT_ban.ban_reason,''), BAT_players.BAT_player, ban_soort
FROM BAT_players LEFT JOIN
BAT_ban ON BAT_ban.UUID=BAT_players.UUID LEFT JOIN
BAT_kick ON BAT_kick.UUID=BAT_players.UUID
ORDER BY ban_id DESC
For MySQL, replace ISNULL with IFNULL.
An INNER JOIN requires a value in both tables, so if you use it for both kicks and bans, you will only see users who have been both kicked and banned.
To show users who have been either kicked or banned, you need to change both joins to LEFT JOIN, which essentially means "join if you can, but don't discard rows if you can't". That will include users who have been neither kicked nor banned, so you will also want an extra condition in the WHERE clause saying BAT_ban.ban_id IS NOT NULL OR BAT_kick.kick_id IS NOT NULL.
Note that where a user has multiple bans and multiple kicks, this will produce a row for every combination of ban and kick, since there is no rule to determine which ban should line up with which kick.
An alternative is to write two queries, each using INNER JOIN, and then combine the results. If you give them the same number and type of output columns (leaving NULL for those which aren't applicable) you can use UNION to run both and return the complete result set in one go.
I need to roll these to into one insert statement. I was thinking something with a full outer join but that doesn't work because I am doing one join with columnA and the other with columnB so I'm not sure ... do I have to go with two insert statements ... because I tried that and I didn't get the results I was looking for.
FROM MainTable RIGHT JOIN SubTable ON MainTable.ColumnA = SubTable.ColumnA
WHERE (((MainTable.ColumnA) Is Null) AND ((Left([SubTable].[ColumnA],3))="001"));
FROM SubTable LEFT JOIN MainTable ON SubTable.ColumnB = MainTable.ColumnB
WHERE (((MainTable.ColumnA) Is Null) AND ((Left([SubTable].[ColumnA],3))="001"));
FYI ... this is coming from an ACCESS insert query that I can't open in SQL View to see exactly what that code looks like
If I remember access correctly, it can't do a full join. This looks like Access' attempt to do so. Doing quick check on google, I found this link that says to change it to a union like wdosanjos suggested: http://office.microsoft.com/en-us/access-help/join-tables-and-queries-HA010096320.aspx
I've been able to make a query using MS Access 2010 that does just what I want but I am having trouble doing so in SQLite3. Here is the Access SQL
SELECT pubacc_lo.*
FROM pubacc_en
LEFT JOIN pubacc_lo ON pubacc_en.call_sign = pubacc_lo.call_sign;
Basically it selects all of the columns in the pubacc_lo table and the rows where the call_sign fields are equal between the tables. This does not select any of the pubacc_en data into the final query in MS Access.
Playing around in SQLite 3, the closest I've gotten was
SELECT * FROM PUBACC_LO, PUBACC_EN WHERE PUBACC_en.call_sign=PUBACC_LO.call_sign
But this statement selects all of the data in the EN table along with the LO table (cross join?). I've tried some left outer joins but haven't had any luck. Any tips would be appreciated!
You should be able to use the same query as you got from Access. SQLite3 does support left outer joins.
As for your query, if you only want the fields from the LO table, then ask for that in your SELECT clause like this:
SELECT PUBACC_LO.*
FROM PUBACC_LO, PUBACC_EN
WHERE PUBACC_en.call_sign=PUBACC_LO.call_sign
but the problem here is that it will only return call_signs with entries in both tables, while the outer join from access will return all rows from PUBACC_EN irrespective of whether there is a corresponding PUBACC_LO entry..
SELECT pubacc_lo.* FROM PUBACC_LO, PUBACC_EN WHERE UBACC_en.call_sign=PUBACC_LO.call_sign
If you only want to select pubbac_lo's field, this is what you can use.
I have a cross tab query that looks like this:
State Building 1 2 3 4 5
NY
SC
FL
The problem I am having is that I want all of the states to show up, regardless of whether or not there is data. So, I need a Left Join. Unfortunately, when I substitute the Inner Join for Left Join in the code, nothing changes. I am just trying to figure out where the problem is coming from, and I think it may be one of the following causes:
The query doesn't know where to pull
the values from (The states are all
listed in a look up but this may
not be where it's looking)
Left Joins don't work on cross tab
queries.
Could someone please tell me what I am doing wrong?
Here's the SQL:
TRANSFORM Nz(Count(Demographics.ID))+0 AS CountOfID
SELECT Demographics.State
FROM Research
INNER JOIN ( Demographics
INNER JOIN [Status]
ON Demographics.ID=[Status].ID
)
ON (Research.ID=Demographics.ID)
AND (Research.ID=[Status].ID)
WHERE ((([Status].Building_Status)='Complete'))
GROUP BY Demographics.State,
[Status].Building_Status
PIVOT Research.Site In (1,2,3,4,5,6,7,8,9,10,11)
Ideally, I could specify the row values in the In statement above (which is currently specifying column values 1-10), but I don't think this can be done.
I didn't completely get your example, but from your comment "I want all of the states to show up, regardless of whether or not there is data", I think you want an "OUTER" join. Outer joins do just that -- they include data regardless of whether or not there is a "match". Inner joins (the default) include data only if there is a match.
Hope this helps,
John
You should change it this way:
TRANSFORM Nz(Count(Demographics.ID))+0 AS CountOfID
SELECT Demographics.State
FROM Demographics
LEFT JOIN ( Research
LEFT JOIN [Status]
ON Demographics.ID=[Status].ID
)
ON (Research.ID=Demographics.ID)
AND (Research.ID=[Status].ID)
WHERE ((([Status].Building_Status)='Complete'))
GROUP BY Demographics.State,
[Status].Building_Status
PIVOT Research.Site In (1,2,3,4,5,6,7,8,9,10,11)
If your problem is that there may be 0 rows in table Demographics with State = 'NY' (for example) but you want to see state 'NY' in the results anyway, then you need another table e.g. States that has all the states in it, and make this your driving table:
SELECT States.State
FROM States
LEFT OUTER JOIN Demographics ON Demographics.state = States.state
...
you can use a two steps solution (it is more readable)
first enclose your actual (and working) sql statment as a view
create view step1 as
TRANSFORM ....
SELECT ...
FROM ...
PIVOT ...
and then create a select like:
(you will need a state table like #Tony Andrews says)
select * -- or whatever
from state
left join step1 on state.id = step1.state
and that's all