JOIN mysql table on PRIMARY KEY and JSON array - mysql

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)

Related

How can i get the same value from same table?

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)

Sql query to Select one result per all matches

I got two tables
Table 1:
id|value
1|Tom
1|Lucy
2|Tom
2|Lucy
3|Tom
3|Lucy
3|Bard
Table 2:
id|value
1|Tom
1|Lucy
2|Tom
2|wrong
3|Tom
3|Lucy
Results should be id where all the values match in both tables:
1
Tried this:
select distinct a.id
from table1 a
join table2 b on a.id=b.id and a.value=b.value
results are
1
2
3
INTERSECT comes to mind. Or a FULL OUTER JOIN maybe. MySQL supports neither.
The easiest way I can think of in MySQL:
select id
from table1
group by id
having (id, group_concat(value order by value)) in
(
select id, group_concat(value order by value)
from table2
group by id
);
Demo: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=768cc8fb2d01c2b5219a4d56d127d117

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

SELECT * FROM table1 LEFT JOIN table1.value AS table2

i have a db like this:
Table1:
id
id_item
table (enum: 'table2','table3','table4')
table2:
id
value
table3:
id
value
table4:
[...]
And i want to run a query like this:
SELECT t1.id, t2.value FROM table1 AS t1
LEFT JOIN table1.table as t2 ON t1.id_item=t2.id
Is it possible? or i have to select first table1 and after the value?
( sorry for my bad eng :) )
If I understood the last column in Table 1 correctly and it is just a string, you can't.
You cannot write a column into the FORM-clausal and wait for mysql to evaluate it for every row and find the correct table to join it with.
To do this you will need to create a view where you will have the data from all the tables, together with the table name as an additional column. Afterwards you can perform a join like that between Table1 and the new view

Delete records from a table where < max number for a field and keep highest number

I know this sounds rather confusing but I'm at a loss how to explain it better. I have a table simplified below:
DB Type ID
================
Table1 1
Table1 2
Table1 3
Table1 4
Table1 5
Table2 6
Table2 7
Table2 8
Table2 9
Table2 10
what i am trying to achieve is to basically clean out this table but keep the record with the highest ID for each DB Type if that makes sense - so in this case it would be (Table1,5) and (Table2,10) with all other records being deleted. Is it possible to do this exclusively through MySQL?
*EDIT***
Answer thanks to tips from Yogendra Singh
DELETE FROM MyTable WHERE ID NOT IN (SELECT * FROM (SELECT MAX(ID) from MyTable GROUP BY DB Type) AS tb1 ) ORDER BY ID ASC
TRY selecting the max ID group by db_type first and then use it as sub query with not in.
DELETE FROM MyTable
WHERE ID NOT IN
(SELECT ID FROM
(SELECT MAX(ID) AS ID from MyTable GROUP BY DB Type) AS tb1
)
EDIT:
DELETE FROM MyTable
HAVING MAX(ID) > ID;
delete your_table
from
your_table left join
(select max(id) max_id from your_table group by type) mx
on your_table.id=mx.max_id
where mx.max_id is null
Subquery returns the maximum id for every type, and those are the values to keep. With an left join i'm selecting all the rows from your table that don't have an in in max_ids, and those are the rows to delete. This will work only if id is primary key, otherwise we have to join also the type.
Is the combination DB Type - ID unique?
If so, you can attack this in two stages:
Get only the rows you want
SELECT [DB Type], Max(ID) AS MaxID
FROM YourTable
GROUP BY [DB Type]
Delete the rest (Wrapping the previous statement into a more complicated statement; don't mean that)
DELETE FROM YourTable
FROM
YourTable
LEFT JOIN
(SELECT [DB Type], Max(ID) AS MaxID
FROM YourTable GROUP BY [DB Type]) DontDelete
ON
YourTable.[DB Type]=DontDelete.[DB Type] AND
YourTable.ID=DontDelete.MaxID
WHERE
DontDelete.[DB Type] IS NULL
DELETE FROM MyTable del
WHERE EXISTS (
(SELECT *
FROM MyTable xx
WHERE xx."db Type" = del."db Type"
AND xx.id > del.id
);
delete from my_Table
where Day in (select MAX(day) d from my_Table where id='id')