now i am working with three tables,A,B and C:
A table structure:
ID,
Name,
Age
B table structure:
ID,
A.ID-> foreign key,
Hospital Name
C table structure:
ID,
A.ID->foreign key,
Drug Name
so the relation between A and B is one to many and also the same with A and C
when i make any query to find how many persons in my database i found duplicated rows
actually its not duplicated but it has for example 2 rows with the child table like: the one person has 2 records in table B so the result doesn't reflect the actual number of matched record because its linked with child tables .
Question is : how to prevent duplication in case like that?
You can use Distinct or a subquery.
SELECT DISTINCT a.ID, a.Name
FROM a
INNER JOIN b
ON a.ID = b.aID
WHERE b.Hospital = 123
Or
WHERE b.Hospital IN ( 123, 456 )
Also
SELECT a.ID, a.Name
FROM a
INNER JOIN (SELECT aID, Hospital FROM b) x
ON a.ID = x.aID
WHERE x.Hospital - 123
And
SELECT a.ID, a.Name
FROM a
WHERE a.ID
IN ((SELECT aID FROM b WHERE Hospital = 123))
Related
I am building a SQL query which compares two tables A and B by a [name] column and returns the names from table A that are not in table B
Example
Table A
ID Name Address
1 A ABC
2 B XYZ
3 C PQR
Table B
ID Name Gender
1 A F
2 B M
3 D F
The query I wrote should return third row from table A as it is not in table B and should exclude all other rows
Following is the query I built
Select * from A oa left join B gp ON oa.name!=gp.name
the above doesn't return the results I was expecting.
Can this be corrected?
Easiest way:
select * from A where name not in (select name from B)
Better way:
select * from A where not exists (select 1 from B where B.name = A.name)
"A left join B" means keeping everything in A, and associating records in B if the condition is satisfied.
In your case, if you really wanna use left join, here is what it should be ('=', not '!='):
Select * from A oa left join B gp ON oa.name=gp.name where gp.name is null
Better way would be using 'not exists' performance-wise, or 'except' if null values are not an issue.
Using excpet operator will help
select * from TableA
except
select * from TableB
SELECT a.*
FROM A a
LEFT JOIN B b
ON a.name = b.name
WHERE b.name IS NULL
Suppose I have table A, B
ID in A is unique but in table B, ID is not unique
I want to SELECT DISTINCT ID
query 1:
SELECT DISTINCT ID FROM A a LEFT JOIN B b ON a.ID = B.ID WHERE ...
query 2:
SELECT DISTINCT ID FROM A WHERE ID IN (SELECT DISTINCT ID FROM B where ...)
or
SELECT DISTINCT ID FROM A a LEFT JOIN (SELECT DISTINCT ID FROM B) b ON a.ID = B.ID WHERE ...
The end result is same but
what happens in query 1 is the space of temp table is more as multiple rows from table B will come with repeated ID
In query 2 i am able to optimize space and further processing as it will have limited rows with all distinct ID's
Isn't there any way to use DISTINCT rows from table B using join and avoiding subqueries?
Actually I have even table C which I will join with this, so I need to care for the number of rows taking part in 2nd join when taking join further with table C.
SELECT DISTINCT ID FROM A a LEFT JOIN (SELECT DISTINCT ID FROM B) b ON a.ID = B.ID WHERE ...
Is this what you want?
Edit so the answer is a bit more visible:
Since your A is unique, but B isn't you can just swap the values :
SELECT DISTINCT ID FROM B b LEFT JOIN A a on a.ID = b.ID WHERE...
I have the following two tables:
Table a:
name qty
a 10
b 20
c 30
d 40
and table b
name qty
a 10
b 20
d 20
e 60.
I want to merge there two tables and create a new table like this
name qty
a 20
b 40
c 30
d 60
e 60
The objective is to add the values if there is have the same value in name or else just append the values in table two to table 1.
Unfortunately, MySQL does not support full outer join. Here is a method using union all and group by:
select name, sum(qty) as qty
from ((select name, qty from a) union all
(select name, qty from b)
) ab
group by name;
To simulate a full outer join, just execute a left outer join (gives all the rows of Table A with all matching rows of Table B or NULL) and a right outer join where Table A is NULL (gives all the rows of Table B that have no match in Table A -- matches are already provided in first query).
In the first query, there will always be a Qty value from Table A with either a Qty value or NULL from Table B. In the second query, there will only be a Qty value from Table B.
See Fiddle results.
select a.Name, a.Qty + IsNull( b.Qty, 0 ) as Qty
from #TableA a
left outer join #TableB b
on b.Name = a.Name
union all
select b.Name, b.Qty
from #TableA a
right outer join #TableB b
on b.Name = a.Name
where a.Name is null;
You may use union or union all with the same results. Since there is less processing required with union all, that's what I chose.
lets say I have 2 tables:
Table 1: (customers)
------------------------------------------
id | name | etc... | etc..
Table 2: (blockList)
------------------------
id
I want to know if each customer exists or not in blockList table as I'm looping thru customers table (in a single query, as a seperate field)
like this: SELECT * FROM customers, blockList ORDER BY id DESC
You need to use join, example :
SELECT c.*, b.id AS id_blocklist
FROM customers AS c
LEFT JOIN blocklist AS b ON b.id = c.id
ORDER BY c.id DESC
If you want only records in blocklist, use INNER JOIN
you must specify tables connection
SELECT * FROM customers as c, blockList as b WHERE c.id = b.id ORDER BY id DESC
My question is a bit dummy, but SQL has never been my cup of tea.
I have a simple table with 3 columns: id, name, parent_id (referring to an id in the same table).
I just want to get the relevant results to display them in an HTML page:
ID / Name / Parent Name
I can query as follows:
select id, name, parent_id from fields
union
select a.id, a.name, b.name
from fields a, fields b
where b.parent_id = a.id;
and do some tests, but i can figure out that it exists some more elegant manner.
Thx.
This query is best achieved by using a LEFT JOIN. This way you can still return fields which do not have a parent_id, i.e. are NULL. You'll also want to select the parent_name using an alias (parent_name in the example). This will allow you to programatically refer to the result by it's name rather than the numerical column index ($row->parent_name vs $row[2] if PHP is your language of choice).
SELECT f1.id, f1.name, f2.name parent_name
FROM fields f1
LEFT JOIN fields f2 ON f2.id = f1.parent_id
http://sqlfiddle.com/#!2/a9632/1/0
SELECT A.id AS ID, A.name AS Name, B.name AS Parent
FROM fields A, fields B
WHERE A.parent_id = B.id