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
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 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.
This is the first time I actually work with MySQL ...
I got the following situation:
I got the tables A and B.
A has a 1:n relation to B.
(B has a foreign key column pointing to A).
The result I need:
All records in A plus the count of all records in B related the respective
record in A.
Example:
What I did so far:
I created the following query:
SELECT A.*, COUNT(*) AS B_count
FROM $db.A AS A JOIN $db.B AS B ON (A.id=B.A_id)
GROUP BY A.id
My problem:
The query I created only returns records from A that have records in B related to them (the ON statement)
Question:
What do I need to do in order to also obtain all records from A that have 0 records in B related to them?
USE LEFT JOIN and Count the B.id, if you dont get match you will have null and count will return 0
SELECT A.*, COUNT(B.id)
FROM TableA A
LEFT JOIN TableB B
ON A.id = B.id
to get rows with 0 counts you have to use LEFT join, like:
SELECT A.*, COUNT(B.id) AS B_count
FROM $db.A AS A LEFT JOIN $db.B AS B ON (A.id=B.A_id)
GROUP BY A.id
I've this table (a)
And this table (b)
Now I have to get all records from A which are not present in B (a.id not present as b.idDomanda) and where B.idUser is not 1. So In this case, it should return only id 2 from a, but it returns 1 and 2.
This is my Query
SELECT a.* FROM a LEFT JOIN b ON a.id=b.idDomanda WHERE ( b.idUser <> 1 OR b.idUser IS NULL ) GROUP BY a.id
You want to move the condition on b to the on clause:
SELECT a.*
FROM a LEFT JOIN
b
ON a.id = b.idDomanda and b.idUser <> 1
WHERE b.idUser IS NULL
GROUP BY a.id;
The group by suggests that you might want to use not exists instead:
select a.*
from a
where not exists (select 1
from b
where a.id = b.idDomanda and b.idUser <> 1
);
There should be no results given your data set.
All records from A which are not present in B (a.id not present as b.idDomanda)
Given the test data set all of A is in fact IN b.idDomanda... even when filtering out userId = 1.
but as the previous person pointed out that is the query to check.
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.