I want to use IN condition, it works with the value not work with the variable of another table, Here is my sample
SELECT A.*,B.*
FROM table1 AS A
LEFT JOIN erp_wh_salhd AS B ON B.InvNo = '15' AND A.InvNo IN (B.filter1)
//Not working Comming only one row for B table
if I pass values directly means it works, i.e
SELECT A.*,B.*
FROM table1 AS A
LEFT JOIN erp_wh_salhd AS B ON B.InvNo = '15' AND A.InvNo IN (2,3)
// Working. table B have two values
If the filter1 field in erp_wh_salhd is a comma separated list of values, you will need to use FIND_IN_SET instead of IN:
SELECT A.*,B.*
FROM table1 AS A
LEFT JOIN erp_wh_salhd AS B ON B.InvNo = '15' AND FIND_IN_SET(A.InvNo, B.filter1)
Do the fact you are using join you could try using =
SELECT A.*,B.*
FROM table1 AS A
LEFT JOIN erp_wh_salhd AS B ON B.InvNo = '15'
AND A.InvNo = B.filter1
The IN clause allows for multiple values. In your first query you only provide one value, however:
AND A.InvNo IN (B.filter1)
That is equal to
AND A.InvNo = B.filter1
So you only join those B rows to an A row where the B row's InvNo is 15 and its filter1 equals A's InvNo.
In your second query you join all B rows whose InvNo is 15 to all A rows whose InvNo is either 2 or 3.
Two completely different queries, you cannot really compare.
Related
I have 2 mysql tables a and b. I need to show all records in a that are not in b using a common column 'ID'. Normally this is pretty straight forward but my problem here is this: I need to put a where clause on table b because I'm not interested in all unmatched records just the ones that meet the table 2 where clause criterion:
SELECT a.ID, a.Description
FROM a
LEFT JOIN b ON a.ID = b.ID
WHERE a.Inactive = 0
AND b.Room = '101'
AND b.ID Is Null
This returns nothing. However if I remove the AND b.Room = '101' part, it displays the expected results. But I need that condition because I need only unmatched records specific to a 'room' and not all unmatched records
Move the conditions involving table b from WHERE to ON clause:
SELECT a.ID, a.Description
FROM a
LEFT JOIN b ON a.ID = b.ID AND b.Room = '101'
WHERE a.Inactive = 0
AND b.ID Is Null
It'll find rows where a does not have a match in b (id matches and room number = 101).
I have two tables. A and B
SELECT projectid, statusid from A;
Results as below:
SELECT statusid, status from B
Results as below:
Now how can I have the result as below to replace all stutusid with the value from table B.
You could use JOIN:
SELECT A.projectid, B.status from A JOIN b ON A.statusid = b.statusid;
EDIT:
I want to have all those status codes replaced by "its meaning" which is from another table B. So "join" wont do this job but only showing me matched results from both A and B table
I don't understand the requirements. This is exactly what join is doing, showing corresponding value from second table. If for example you don't have the specific code you should use LEFT OUTER JOIN
SELECT A.projectid, COALESCE(B.status, 'Unknown') AS status
from A
LEFT JOIN b ON A.statusid = b.statusid;
Maybe you are searching for correlated subquery(but it will be literally the same as JOIN) if there is no duplicates on table B
SELECT A.projectid,
(SELECT B.status FROM B WHERE A.statusid = b.statusid) AS status
FROM A
-- if it returns error query returned more than one row then b.statusid is not unique
SELECT A.projectid,
(SELECT B.status FROM B WHERE A.statusid = b.statusid ORDER BY ... LIMIT 1) AS status
FROM A
I have two tables that I am applying a join to. Table A has a foreign key that references rows from Table B. SQL is as follows:
SELECT *
FROM TableA AS a
LEFT JOIN TableB AS b ON a.id = b.tableAId
WHERE a.ownerId = X
I am getting the desired result except for one thing. That is when returning the rows in JSON, only one id column is shown (TableB).
Instead I want to be able to return all id columns in the JSON where duplicate columns would have a number appended to it. For example: id, id1, id2, id3 etc...
You need to specify the columns that you want, explicitly giving them aliases so the names are different. Something like this:
SELECT a.*, b.id as b_id
FROM TableA a LEFT JOIN
TableB b
ON a.id = b.tableAId
WHERE a.ownerId = X;
I have two tables lets say
Table A
columns id , name address
Table B
columns id , age, import_date
The Table B id is a reference key of Table A.
Now I want to return results from A & B but if the record is not in B I still want to see the record so for this I use left outer join
Select * from A a left join B b
on a.id = b.id
Now even I don't have record in B I still get the record.
Table B may contain duplicate ids but unique import_date.
Now I want to results in a way that if there is duplicate id in table B then I want to get the records only where import_date is as of today.
I still want to get the records for ids which are not there but if the ID is there in table B then I want to apply above condition.
I hope someone can help me with this.
Sample data
Table A
01|John|London
02|Matt|Glasgow
03|Rodger|Paris
Table B
02|22|31-AUG-2015
02|21|30-AUG-2015
02|23|29-AUG-2015
The query will return
01|John|London|null|null|null
02|Matt|Glasgow|22|31-Aug-2015
03|Rodger|Paris|null|null
You almost got the solution. Just add one more condition like below
Select a.id,a.name,a.address,b.age,b.import_date
from tablea a left join tableb b
on a.id=b.id and b.import_date=trunc(sysdate)
order by a.id;---This line optional
Check the DEMO HERE
SELECT *
FROM Table_A t1 LEFT OUTER JOIN Table_B t2 ON t1.id=t2.id UNION
SELECT *
FROM Table_A t1 LEFT OUTER JOIN Table_B t2 ON t1.id=t2.id
GROUP BY t2.import_date
HAVING t2.import_date=CURDATE();
I am trying to fetch records from 2 tables mapped by an id where on the second table there may be a row that is missing.
I have a column called name on the second table which contains a string value. The value I need to extract is 'subscriptions' but this does not always exist in the table. There is the possibility to have different values within this column which I do not want to extract.
Is it possible to check to see if the value exists and if it doesn't output null to all the fields.
So far I have this which returns all the records
select COUNT(*)
from PUser a, PAttribute b
where exists (select null
from PAttribute c
where c.name = 'subscriptions' or c.name is null)
and a.id = b.userid;
Hope that explains it.
EDIT
PUser table
id
other columns
PAttribute table
userid mapped to PUser.id
name
Now a userid can have multiple rows each with a different value in name eg, 'subscriptions', 'source', 'etc' 'etc'
I want to fetch all users who have the value 'subscriptions' in the name column or if the row doesnt exist with the value 'subscriptions' as they may not have any.
If they don't have this row the output should be null.
EDIT 2:
Worked this out and I needed
select COUNT(*),(select b.stringValue from PAttribute b where b.userid = a.id and b.name = 'subscriptions') from PUser a order by a.id desc;
Your example is using implicit joins, which are inner joins. This means that a result will only be returned if a row exists in both tables. Instead, you need to use a left join. Change your query to this:
select COUNT(*)
from PUser a LEFT JOIN PAttribute b ON a.id = b.userID
where exists (select null
from PAttribute c
where c.name = 'subscriptions' or c.name is null);
Or (not exactly sure what your desired behavior is), this might work for you:
SELECT count(*)
FROM PUser a LEFT JOIN PAttribute b ON a.id = b.userID
WHERE b.name = 'subscriptions' OR b.name IS NULL;
If you want to exclude rows that do not contain 'subscriptions', you could use the JOIN ON form and in order to keep rows from PUser even there is no matching row from PAttribute with name set to 'subrciptions', and thus obtaining null fields, exploit OUTER JOIN.
select COUNT(*)
from PUser a OUTER JOIN PAttribute b ON ( a.id = b.userid AND b.name = 'subscriptions' )
;
This is a little bit different from your query: EXISTS is less perfomant and, moreover, the SELECT in the EXISTS does search for a row in PAttribute with name equal to null, that is quite different from handling missing rows.