I have 2 table
Columns inside table_A : data, id_A, id_B, status
Columns inside table_B : id, username
I want to display username from table_B reference from columns inside table_A (id_A & id_B) and make the alias id_A as User_1 and id_B as User_2
I has been working with INNER JOIN but it still make me confused
Can you try this...
SELECT a.username as User_1, b.username as User_2 FROM
table_A t
JOIN table_B b on b.id = t.id_B
JOIN table_B a ON a.id = t.id_A
Related
I want to join 2 tables and for each joined result I need a separate row, but the rows of my query result are somehow mixed:
Schema + Data:
My Query:
SELECT Table_A.id,
Table_B.id,
Table_C.id
FROM Table_A
LEFT JOIN Table_B
ON Table_B.id_a = Table_A.id
LEFT JOIN Table_C
ON Table_C.id_a = Table_A.id
The Result:
What I want (3 separate rows - separated by the join-tables' id-column):
Q: What am I doing wrong?
=> You could put it in that way: I need all rows of where id_a is 1 + table_a.id=1. Maybe JOIN is the wrong approach here..?
You need INNER JOIN UNION :
SELECT A.id A_Id, B.id B_Id, ''c_id
FROM Table_A A
INNER JOIN Table_B B ON B.id_a = A.id
UNION ALL
SELECT A.id A_Id, '' B_Id, c.id c_id
FROM Table_A A
INNER JOIN Table_C C ON A.id = C.id_a
table_a contains all orders, while table_b contains only special orders. Every order in each table has a code_field. All orders in table_b are also in table_a, but of course not all orders in table_a are also in table_b. I need to extract all orders in table_a that are not also in table_b. Looking for a solution but I actually cannot figure out how to write it.
You can select everything from table_a and left join table_b by code_field and wherever you don't have matching order in table_b the fields will be null
SELECT table_a.*
FROM table_a
LEFT JOIN table_b
ON table_a.code_field = table_b.code_field
AND table_b.id IS NULL
You can do it with NOT EXISTS:
SELECT *
FROM table_a
WHERE NOT EXISTS (
SELECT 1 FROM table_b
WHERE table_a.code_field = table_b.code_field
)
I'd like to bring in just the most recent date row along with other fields from Table B that would include all values for multiple columns in Table A, even if there is no value in Table B.
The following query works, but only returns values that exist in both Table A AND in Table B. How do I include all of A and the most recent date row of B?
Many Thanks!
SELECT A.person_id,
A.second_field,
B.create_timestamp,
B.second_field
FROM (
SELECT B.person_id, max(B.create_timestamp) as create_timestamp
FROM Table_B B
GROUP BY B.person_id
) BMaxDate
LEFT JOIN Table_B B
ON BMaxDate.person_id = B.person_id AND
BMaxDate.create_timestamp = B.create_timestamp
LEFT JOIN Table_A A ON BMaxDate.person_id = A.person_id
You have to use Table_A as the first table in the LEFT JOIN operation chain:
SELECT A.person_id,
A.second_field,
B.create_timestamp,
B.second_field
FROM Table_A AS A
LEFT JOIN (
SELECT B.person_id, max(B.create_timestamp) as create_timestamp
FROM Table_B B
GROUP BY B.person_id
) AS BMaxDate ON BMaxDate.person_id = A.person_id
LEFT JOIN Table_B AS B
ON BMaxDate.person_id = B.person_id AND
BMaxDate.create_timestamp = B.create_timestamp
If you want all from table_A and use a left join you have to put table_A first.
And for Sql Server you can use the with clause for more readable sub query. Mysql doesn't support the with clause.
With(
SELECT B.person_id, max(B.create_timestamp) as create_timestamp
FROM Table_B B
GROUP BY B.person_id
) AS BMaxDate
SELECT A.person_id,
A.second_field,
B.create_timestamp,
B.second_field
FROM Table_A AS A
LEFT JOIN BMaxDate ON BMaxDate.person_id = A.person_id
LEFT JOIN Table_B AS B ON BMaxDate.person_id = B.person_id
AND BMaxDate.create_timestamp = B.create_timestamp
How con i find how many rows don't have an inner join relationship?
TABLE A
id | name | idpoints
TABLE B
id | point
any record on table A should have a relative record on table B
how can i find how many records on able A don't have a record on table B?
Use left join to join the tables and then take all records where the id in tableB is null
select count(*) from tableA left join tableB on idpoints = tableB.id where tableB.id is null
You can do the following
SELECT COUNT(1)
FROM TABLE_A A
LEFT JOIN TABLE_B B
ON B.ID = A.IDPOINTS
WHERE B.ID IS NULL
here is my problem:
I have two tables. Every entry of table A has several entries in table B matched over an ID. I now want to get all entries of table A with one data entry of table B - the one with the highest ID in this table.
Table A has an ID
Table B has an own ID and ID_OF_TABLE_A (for the relation between both)
Table A has one to many relation to Table B. I want all Entries of Table A, matched with the one with the highest ID out of B. Is there any way to realize this in an SQL Statement? I tried all kinds of joins since I need the information of that matched entry in the outcome of the select.
How about
SELECT *
FROM tableA a
INNER JOIN tableB b ON a.ID = b.ID_OF_TABLE_A
WHERE b.ID = (SELECT MAX(ID) FROM tableB c WHERE b.ID = c.ID)
Try this:
SELECT a.*, b.*
FROM tableA a
LEFT OUTER JOIN (SELECT b.*
FROM tableB b
INNER JOIN (SELECT ID_OF_TABLE_A, MAX(ID) bID
FROM tableB
GROUP BY ID_OF_TABLE_A
) c ON b.ID_OF_TABLE_A = c.ID_OF_TABLE_A AND b.ID = c.bID
) AS b ON a.ID = b.ID_OF_TABLE_A;
You can use an inline view where you filter the rows you need from table b with the help of the grouping clause and max function like so:
select a.*, b.*
from a
join (
select max(id) as id_b_max, id_a
from b
group by id_a
) b
on a.id = b.id_a;
Tested with:
create table a(id int);
create table b(id int, id_a int);
insert a values (1);
insert b values(1, 1);
insert b values(2, 1);
insert b values(3, 1);
select a.id, max(b.id)
from table_a a join table_b b on a.id = b.table_a_id
group by a.id
This should work. prefilter out the max id of the ID_OF_TABLE_A. and then join on that id.
SELECT A.*, B.*
FROM A
INNER JOIN ( SELECT max( ID ) AS id, ID_OF_TABLE_A
FROM B
GROUP BY ID_OF_TABLE_A) AS grp_b
ON grp_b.ID_OF_TABLE_A = a.ID
INNER JOIN B ON b.ID = grp_b.id