Specific SQL Query - mysql

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

Related

SQL query to get 2 values from table 1 and join all possible option from table 2

I want to get value from table1 and join all matching value from table2. The table1 has to be limited to 2 rows, but expecting output should own all matching values for those two ids.
How can I achieve this?
You would use a subquery:
select t1.*, t2.*
from (select t1.*
from table1 t1
limit 10
) t1 left join
table2 t2
on t1.id = t2.table1_id;
Note: This returns two arbitrary rows. Normally, you would have an order by to better specify the rows. And use order by rand() for random rows.
if you want all the value in join for only two row of table 1 you can use a subqiuery with limit 2
select b.id, a.value, b.value2, b.table1_ID
from (
select * from table1
limit 2
) a
inner join table2 on aid = b.table1_ID

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).

Retrieve results from a table based on conditions

I have a query with SQL as it needs an expertise. I have two tables Table A and B. Now I need to retrieve results from table 1 based on some conditions and i also need to retrieve results from table 1 based on results from table 2.
I want to achieve
Select * from table1 where author ="xyz") + select * from table1 where id=""
--->id = select post_ID from table2 where author = "abc"
So the ID values of table1 matches the post_ID values of table 2
You could use OR condition
select t1.* from table1 t1 where author ='xyz'
or exists ( select 1 from table2 t2 where t2.post_ID=t1.id)
Try UNION to merge the results and IN to compare with post_ID value of table 2. Below code might help you.
Select * from table1 where author ="xyz"
UNION
select * from table1 where id IN (select post_ID from table2 where author = "abc")

Connect three tables with a single SQL-Query

If I have this three tables:
table1: id, title, content
connection: id_t1, id_t2
table2: id, title, content
In my case I just select a single row of table1. For this result there are many rows in table2. The connection of both tables can be found in the table 'connection'
How do I have to create the query to get this result?
table1-title
table2-content1
table2-content2
table2-content3
table1-content
If I understand correctly I believe you want to use GROUP BY with the GROUP_CONCAT function
The query would look something like this:
SELECT table1.title, GROUP_CONCAT(table2.content) as table2.group_content, table1.content
FROM table1
JOIN connection on table1.id = id_t1
JOIN table2 on connection.id_t2 = table2.id
GROUP BY table2.content
This would give you one row for each table1.id, with multiple table2.content rows concatenated into one column (called table2.group_content in this example).
select title from table1 where title_id = 1
UNION
select t2.content
from table2 t2, table1 t1, connection c
where t1.title_id = 1
and t1.title_id = c.id_t1
and c.id_t2 = t2.title_id
UNION
select content from table1 where title_id = 1

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)