mysql - query example request - mysql

Updated
Hello all,
MySQL here.
Let's say we have 3 tables.
TableA, TableB, TableC.
TableB relates with foreign keys, TableA and TableC.
I would like to:
List some data from TableA and TableC BUT, that data should be ordered by some column of TableB.
Can I have an example of the above so that I can study it and try to transform to my needs?
Thanks a lot.
MEM

mysql allows you to order by colums that not selected, so you can join your tables
select ta.somefield, tc.somefield
from TableA ta INNER JOIN TableC tc on tc.somefield=ta.somefield
INNER JOIN TableB tb on tb.somefield=ta.somefield
ORDER by tb.somefield

SELECT TABLEA.fieldnames, TABLEC.fieldnames FROM TABLEA, TABLEB, TABLEC WHERE TABLEA.PRIMARY=TABLEB.TABLEA_PRIMARY AND TABLEC.PRIMARY=TABLEB.TABLEC_PRIMARY ORDER BY TABLEC.fieldname DESC

select
tA.blah,
tA.goop,
tC.schmarr,
tC.broigle
from
tB
join tA on tA.joincol1 = tB.joinCol1
join tC on tC.joinColx = tB.joinColx
order by
tc.schmarr

Related

How to join 3 tables where each has the key to the next in line

Imagine the following scenario:
There are 3 tables A, B and C.
Table A has no knowledge of either table B and table C.
Table B has a foreign key to table A.
Table C has foreign key to table B.
In table B as well as in table C there can be multiple items sharing the same foreign key value.
As you can see, the items from C are indirectly referenced to A through B.
What I want is to get all entries from A that are referenced in C but without any information from B or C in my result tables and without duplicates.
Is this even possible?
I have tried this like so but have no idea if it is correct:
select tableA.*
from tableA,
(select distinct tableB.AId as Aid
from tableB left join tableC on tableC.BId = tableB.id
group by tableB.id)
as temp
where tableA.id = temp.Aid
I am not sure if I understand it correctly, but you can try this one:
SELECT DISTINCT `A`.`id`, `A`.`value1`, `A`.`value2` FROM `A`
INNER JOIN `B` ON `B`.`id-a` = `A`.`id`
INNER JOIN `C` ON `C`.`id-b` = `B`.`id`
It returns all values from table A if there is a key on Table C which is linked to Table B with corresponding foreign key on table A
An alternative approach to Masoud's good response would be to use an exists though a correlated subquery.
The below subquery joins B to C in a correlated fashion (notice the B.IDA to A.ID and A is outside the subquery).
If we assume good database design, then A will not have duplicate records, thus we can omit a distinct here since we are not joining A to the other tables. Instead we are simply checking for the existence of an "A" record in the B table which must have a record in the C table due to the inner join. This has two advantages for performance
It doesn't have to join all the records together which would then
necessitate a distinct; thus you don't have the performance hit on
the distinct.
It can early escape. once a key value of A is found in the
subquery (B to C join) , it can stop looking and thus don't have to join all of B to all of A.
We select "1" in the subquery as we don't care what we select as the value will not be used anywhere. We're just using the coloration of A to (B JOIN C) to determine what in A to display.
SELECT A.*
FROM A
WHERE EXISTS( SELECT 1
FROM C
INNER JOIN B
on C.IDB = B.ID)
AND B.IDA = A.ID)
Taking what you tried and reviewing it:
select tableA.*
from tableA,
(select distinct tableB.AId as Aid
from tableB left join tableC on tableC.BId = tableB.id
group by tableB.id)
as temp
where tableA.id = temp.Aid
Starting with the "FROM"
You have tableA, (subquery) temp. This is a CROSS JOIN meaning all records from A will be joined to ALL records of (B JOIN C) so if you have 1000 records in A and 1000 records in the temp result then you'd be telling the database engine to generate 1000*1000 records in your result set; which then gets filtered to only include records matching in temp and A. The engine may be smart enough to avoid the cross join and optimize the query, but I find it confusing to maintain. So I would rewrite as
SELECT tableA.*
FROM tableA
INNER JOIN (SELECT distinct tableB.AId as Aid
FROM tableB left join tableC on tableC.BId = tableB.id
GROUP BY tableB.id) as temp
ON tableA.id = temp.Aid
Looking at the subquery (temp)
We don't need a group by as we are not aggregating. The distinct does bring us down to 1 record but at a cost to execution time.
So I would re-write as this:
SELECT tableA.*
FROM tableA
INNER JOIN (SELECT distinct tableB.AId as Aid
FROM tableB
LEFT JOIN tableC
on tableC.BId = tableB.id) as temp
ON tableA.id = temp.Aid
Then looking at the whole, if we change the outer query join to temp and make it an exists... using coloration we don't have the performance hit of the join, nor the distinct. and I'd switch the left join to an inner as we only want records in C and B so we'd have null in B if we left it as a "LEFT JOIN" which serve no purpose for us.
This gets me to the answer I initially provided.
SELECT tableA.*
FROM tableA
WHERE EXISTS (SELECT 1
FROM tableB
INNER JOIN tableC
on tableC.BId = tableB.id
AND tableB.AID = A.ID) as temp

MySQL - Selecting rows where fields not equal

I have a little problem with an SQL query: I have 'TableA' with a field 'TableA.b' that contains an ID for 'TableB'. I want to select all rows from 'TableB' that don't have an ID that equals any field 'TableA.b'. With other words, I need every row from TableB that's not referred to by any row from TableA in field .
I tried a Query like this :
SELECT DISTINCT TableB.* FROM TableA, TableB Where TableA.b != TableB.ID
But the result contains a row that is also returned by the negation, i.e. where both fields have the same value.
Any ideas?
What you need is LEFT (or RIGHT) JOIN.
SELECT TableB.* FROM TableA
LEFT JOIN TableB on TableA.b = TableB.ID
WHERE TableA.b IS NULL
While it's possible to do the same with a subquery as in some of the otehr answers. A join will often be faster.
A LEFT [OUTER] JOIN can be faster than an equivalent subquery because
the server might be able to optimize it better—a fact that is not
specific to MySQL Server alone. Prior to SQL-92, outer joins did not
exist, so subqueries were the only way to do certain things. Today,
MySQL Server and many other modern database systems offer a wide range
of outer join types.
First, select all ids from TableA:
SELECT DISTINCT b FROM TableA
Then use that result to select all rows in TableB that have an id that does not exist in this set by using the above query as a subquery:
SELECT * FROM TableB WHERE ID NOT IN (SELECT DISTINCT b FROM TableA)
Hope this helps.
You can try this
SELECT TableB.* FROM TableB
WHERE ID NOT IN
(SELECT b from TableA);
Use NOT IN in SELECT Query.
SELECT * FROM TableB t1 WHERE t1.ID NOT IN (SELECT t2.b FROM TableA t2);
You can use right join also.
Try this:
SELECT DISTINCT TableB.* FROM tablea RIGHT JOIN TableB ON TableA.b = Tableb.ID WHERE TableA.B IS NULL

Select records from one table where a column value exists in another table

I have 2 MySQL tables A and B.
I would like to select only the records from B where a certain value exists in A.
Example:
A has columns: aID, Name
B has columns: bID, aID, Name
I just want the records from B for which aID exists in A.
Many thanks.
You need to do either INNER JOIN - records that exists in both tables, or use LEFT join, to show records that exists in A and matching IDs exists in B
A good reference:
You need to make a join, and if you don't want to retrieve anything from table b, just return values from table a.
This should work
select b.* from b join a on b.aID=a.aID
Below query will also work and will be effective
SELECT * FROM B
WHERE B.aID IN (SELECT DISTINCT aID FROM A)
You just need a simple inner join between tables A and B. Since they are related on the aID column, you can use that to join them together:
SELECT b.*
FROM tableB b
JOIN tableA a ON a.aID = b.aID;
This will only select rows in which the aID value from tableB exists in tableA. If there is no connection, the rows can't be included in the join.
While I recommend using a join, you can also replace it with a subquery, like this:
SELECT *
FROM tableB
WHERE aID NOT IN (SELECT aID FROM tableA)
You can use join like this.
Select b.col1,b.col2... From tableB b inner join table tableA a on b.field = a.field
Have you tried using a LEFT JOIN?
SELECT b.* FROM tableB b LEFT JOIN tableA a ON b.aID = a.aID

Populating new DB table from other table data

Hi there basically i have 2 tables with 162 entries and im trying to populate a new table with entries from the other tables to show the difference between a number value
Insert Into popdiff(
popdiff)
select (a.malepop+a.femalepop)-(b.malepop+b.femalepop)
from tablea a, tableb b;
The problem im having is it is returning 26244 results i.e. 162*162 rather than 162 which im expecting, having looked into it a bit the query is finding the value for each entry in tablea- the 162 values in tableb
how can i simply return just the 162 rows?
You didn't specify the relationship between the two tables and that's a cross join which returns what you described. Specify the relationship either in a WHERE condition or a JOIN condition.
WHERE:
SELECT (a.malepop+a.femalepop)-(b.malepop+b.femalepop)
FROM tablea a, tableb b;
WHERE a.id = b.id;
JOIN:
SELECT (a.malepop+a.femalepop)-(b.malepop+b.femalepop)
FROM tablea a
INNER JOIN tableb b ON a.id = b.id;
Of course, you may have different names for the IDs, so change the a.id = b.id consition accordingly.
You have to do an inner join:
select (a.malepop+a.femalepop)-(b.malepop+b.femalepop)
from tableA join tableB using (field)
That will do the job

SQL JOINING, Which is the best join

I'm trying to collate data from three tables.
tableA, tableB and tableC. Table A is my main table containing the most data I want to pull.
Table A joins to Table B via a.id and b.new_id. However, Table C does not have any association to Table A and joins only through Table B: b.board_id and c.board_id.
Which is the best way to join these tables so that the variables will represent table A?
I can connect tables A and B like this:
SELECT * FROM tableA a LEFT JOIN tableB b ON a.id = b.new_id and the variables are fine. But when I try to add TableC the variables shown are that of TableC, not TableA.
Some of the tables contain identical column names. How would I join all three?
It must be like this:
SELECT * FROM tableA a LEFT JOIN tableB b ON a.id = b.new_id join tableC c on c.board_id=b.board_id
You query should look like this:
SELECT a.col1 as a_col1, a.col2 as a_col2, . . .
FROM tableA a LEFT JOIN
tableB b
ON a.id = b.new_id left join
tablec c
on b.board_id = c.board_id;
You don't have to create a separate name for all columns, you only need to do it for the ones with duplicates.