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")
Related
Hey guys I have a long table in my Database and i want select all records that have the same id and parent_id.
id
name
parent_id
2
lorem
2
Second case:
In the second case there are ids and parent_ids in different rows.
Thanks in advance.
SELECT ID,
PARENT_ID
FROM TABLENAME
WHERE ID=PARENT_ID
I think you can use this structure:
SELECT [column list] FROM [tablename] WHERE id=parent_id
you simply need to put an = sign for the column that you want to have the same values. Passing this condition to the where clause will filter the rows to show only the ones where the two columns are equal.
select * from <your_table_name> where id = parent_id
<your_table_name> = pass your table name
for your case 2 where you want the rows that matches the id = parent_id but they are not in the same row:
you need a self-join
SELECT
*
FROM
<table_name> as t1
INNER JOIN <table_name> as t2
on t1.id = t2.parent_id
if in different tables:
SELECT * FROM table1 WHERE parent_id in (SELECT id FROM table2)
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).
I have a Table, table1 with column
id name
1 A
2 B
3 C
4 D
And another table with column
id group
1 ["1","3"]
2 ["2","3"]
3 ["1","4"]
group is a JSON type field. I want to get records from first table according to the second table groups.
SELECT * FROM table1 WHERE id IN (SELECT group FROM table2 WHERE id=1);
I tried the following query but not getting the result.
SELECT * FROM table1 WHERE JSON_CONTAINS(id, (SELECT group FROM table2 WHERE id=1))
Wrong argument order. According to docs JSON should be the first argument.
SELECT * FROM table1 WHERE JSON_CONTAINS((SELECT group FROM table2 WHERE id=1), id)
Your JSON array contains strings, while you're looking for id that is INT. Try something like this:
SELECT * FROM table1 WHERE JSON_CONTAINS((SELECT `group` FROM table2 WHERE id=1), JSON_QUOTE(CAST(id as CHAR(50))))
Try this query
SELECT * FROM table1 WHERE id IN(SELECT JSON_EXTRACT(group) As id FROM table2 WHERE id= 1)
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
I am trying to exclude rows in my table based on the id's in other tables.
I have 2 tables of which a "select * from" results in a set like (1,2,3)
I am trying to combine the results from these 2 subqueries into one, like:
(1,2,3) + (4,5) = (1,2,3,4,5)
So I can filter the big table with a "NOT IN (1,2,3,4,5)"
I have been looking at GROUP_CONCAT's, UNION and all other kinds, but I can't seem to find something that actually works.
Anyone have a idea?
select *
from Table3
where id not in (
select id from Table1 --your subquery that returns 1,2,3
union all
select id from Table2 --your subquery that returns 4,5
)
select * from mytable
where id not in (
select id from othertable
union
select id from othertable2
)