sql join request - mysql

Well, i'm trying to make a sql request with join but i don't know how to do it.
Here my first table - Table 1
id
postid
user
My second table - Table 2
id
title
Postid and id are the same.
i've done a screenshot of my table 1
As you can see, there are many entries with postid 32. This is totally normal.
I want to do a sql request on this 2 tables.
The results expected have to be like this :
Title of id 31 (from table 1) - 2 (because there are 2 entries with postid 31 in table 2)
Title of id 32 (from table 1) - 23 (because there are 23 entries with postid 32 in table 2)
Someone can help me ?

Try this:
select t1.postid, count(t2.id)
from Tab1 t1 join Tab2 t2
on t1.postid = t2.id
group by t1.postid;
Here the name of the tables are Tab1 and Tab2 and they have aliases t1 and t2.

If you are interested only in the postids of table1 then there is no need for a join:
select postid, count(*)
from table1
group by postid;
If you want to count all the ids of table2 even the ones that are missing in table1 then you need a left join:
select t2.id, count(t1.postid)
from table2 t2 left join table1 t1
on t1.postid = t2.id
group by t2.id;

Related

How do I search relations on tables SQL

I have 3 tables like this
With the tables filled like this:
How do I search the cases In where on the table 3 the idtable 1 has all the id from table 2 related?
For example idtable1 = 1 would be an output of that query cuz is related with every id from idtable2
Presumably, you intend:
select t1.*
from table1 t1
where (select count(*) from table3 t3 where t3.idtable1 = t1.idtable1) =
(select count(*) from table2);
This shows all records from table1 where table3 contains all values of idtable2 -- assuming no duplicates in table3 (and that the ids are unique).

Which MySQL JOIN and How...?

I have two tables:
Table 1 contains the User ID
Table 2 contains the user ID and other data I would like
The relationship is on the ID in both tables so what I would like to do is the following:
Pull all data from table 2 where a record exists in the id field in table 2 that matches an id in table 1.
Table 1 has other copies so to speak that are specific to other accounts while table 2 contains all the ids for all the other tables which is why (I think) I need a JOIN statement but I'm open to suggestions.
Table 1:
id
123456
Table 2:
id | name | age
123456 | John | 23
651123 | Mary | 22
811561 | Sarah | 21
You can use subquery as:
SELECT *
FROM table2
WHERE ID IN (SELECT ID
FROM table1)
If you need fields from table1 as well then use an inner join like:
SELECT t1.*, t2.name, t2.age
FROM table1 t1 INNER JOIN table2 t2
ON t1.id = t2.id
Your assumption is correct, you need to join:
Select * from table1 inner join table2 on table1.userid = table2.userid
The only question here is if you want to get only id's that appear on both tables (and than use inner join) or also get such that appear only on the first table as well (left join)
You should choose inner join here because table 2 always contain records for table 1.
You can use INNER JOIN here becuase you have relation between both table, query should be this:
SELECT t1.id, t2.name, t2.age
FROM table1 t1
INNER JOIN table2 t2 ON t1.id = t2.id

Specific SQL Query

I have been trying to find the correct query for this problem but it doesn't quite work so I'm asking here:
I have 2 tables:
Table-1 has 5 rows with 6 attributes each
Table-2 has 3 rows with 5 attributes and EACH attribute corresponds to 1 row from table 1. What I want is a query that will give me all the attributes from table 1 that are contained in table 2. I've come up to this:
SELECT *
FROM Table1
WHERE PrimKey IN
(SELECT *
FROM Table2
WHERE PrimKey = Index)
However it won't let me do this because it says that on the second SELECT I can't select all but I have to choose. This way I can only view 1 row of Table1 stuff from Table2 but I want to view all of Table2's attributes.
SELECT *
FROM Table1
WHERE PrimKey IN
(SELECT PrimKey
FROM Table2)
Or INNER JOIN
SELECT t1.*
FROM Table1 t1 INNER JOIN Table2 t2
ON t1.ReferencingColumn = t2.ReferencingColumn

MySQL select fields in one table that are not in another table

I have 2 tables in a MySQL DB:
Table 1 : id, galleryname
Table 2 : galleryid, <many other fields...>
Using PHP, I need to select all rows in Table 1 based on its ID where that id (galleryid) does not appear in Table 2.
Example:
Table 1
1, flowers
2, water
3, mountains
4, winter
Table 2
3, ...
would return these rows from Table 1
1, flowers
2, water
4, winter
I'm not exactly sure how to go about this. I am pretty good at the basics of MySQL but I suspect this is a JOIN or a UNION that is out of my league.
Any help is appreciated.
Try this:
SELECT * FROM table1
WHERE id NOT IN
(SELECT galleryid FROM table2)
or
SELECT * FROM table1
LEFT JOIN table2
ON table1.id = table2.galleryid
WHERE table2.galleryid IS NULL
Left join brings all the t1 records, then filter out those that have t2.galleryid NULL (no records in t2)
SELECT id, galleryname
FROM table1 AS t1
LEFT OUTER JOIN table2 AS t2 ON t1.id = t2.galleryid
WHERE t2.galleryid IS NULL
SELECT * FROM table1
LEFT JOIN table2
ON table1.id = table2.galleryid
WHERE table2.galleryid IS NULL
Someone posted an answer (then deleted it) that gave me ONLY the record that was in both. All others here seemed to give errors but using that original post I made one change and it worked.
Original:
SELECT * FROM table1 INNER JOIN table2 ON galleries.id = images.galleryid
(this gave me just the one that was in both)
Adding the !:
SELECT * FROM table1 INNER JOIN table2 ON galleries.id != images.galleryid
(this gave me what I needed)

Mysql Query optimization

Below is my Table Structure
Table 1
id
name
Table 2
id
table1_id
I want the rows from table 1 which have no reference value in table 2.
Example data:
Table 1
id name
1 demo
2 demo2
3 demo3
Table 2
id table1_id
1 1
2 1
3 1
4 3
5 3
So there is no value in table 2 with table1_id 2. I want id 2 from Table 1.
Below id the query i have tried:
SELECT l.id FROM Table1 l WHERE l.id NOT IN (SELECT DISTINCT(r.id) FROM table2 r);
This is returning a proper result but its taking more than 2 minutes to process.
In table 1 i have 4000 rows and in table 2 I have 40000 rows.
Any optimisation to above query or any alternative solution?
SELECT * FROM table1 LEFT JOIN table2
ON table1.id=table2.table1_id
WHERE table2.table1_id IS NULL
Have an index for Table1.id and Table2.table1_id, then try the following query:
SELECT Table1.id FROM Table1
WHERE Table1.id NOT IN (SELECT Table2.id FROM Table2 group by Table2.table1_id);
What you are trying to acheive is to find orphan records right?
A join that shows all the records from the first(the left) table and the matching values form the other or nulls for no matches is called a left join. I think a left join will do the same job but it is not going to be any faster. Joins are in general slower.
I found a place where it is all well explained - http://explainextended.com/2009/09/15/not-in-vs-not-exists-vs-left-join-is-null-sql-server/
It does not hurt to try with a join though, and tell us were your results the same as expected.
select t1.id from table1 as t1
left outer join table2 as t2
on t2.table1_id = t1.id
where t2.id is null;
or
select t1.id from table1 as t1
where not exists (select 1
from table2 as t2 where t2.table1_id = t1.id);