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)
Related
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")
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 am having problem to find records from two tables where both the tables have common name field and both fields have comma separated values.
For instance,
table-1 have "a,b,c" value
id | name
----------
1 | a,b,c
and table-2 have "a,c,d,e,f" value
id | name
---------------
1 | a,c,d,e,f
Now I want to compare both tables where at least one character matches in both.
So is it possible to get records where at least one character matches into both fields or not?
Thanks in advance :)
First, use the UNION statement to combine rows in both tables; include only the columns that need to compare. The returned result set is used for the comparison. Considering table-1 as t1 and table-2 as t2.
SELECT t1.pk, t1.c1
FROM t1
UNION ALL
SELECT t2.pk, t2.c1
FROM t2
Second, group the records based on the primary key and columns that need to compare. If the values in the columns that need to compare are identical, the COUNT() returns 2, otherwise the COUNT() returns 1.
See the following query:
SELECT pk, c1
FROM
(
SELECT t1.pk, t1.c1
FROM t1
UNION ALL
SELECT t2.pk, t2.c1
FROM t2
) t
GROUP BY pk, c1
HAVING COUNT(*) = 1
ORDER BY pk
MySQL compare two tables example
SELECT id,title
FROM (
SELECT id, title FROM t1
UNION ALL
SELECT id,title FROM t2
) tbl
GROUP BY id, title
HAVING count(*) = 1
ORDER BY id;
It will return unmatched records.
I have four tables with same fields. Now I want to join these tables in such a way that I retrieve records only if there is a match between any two tables on a field(like name).
Thanks in advance.
This would return all the name values that appear in more than one table:
select
name
from
(select distinct
name
from table1
union all
select distinct
name
from table2
union all
select distinct
name
from table3
union all
select distinct
name
from table4) temp
group by name
having count(*) > 1;
Check out the interactive example.
I have two tables a and b which has a field name in it.
I need to list the data from these two tables. I thought of using union but in the result list data from the first table appears and then followed by the second.
what i want is to order by the field name so the result should be a mixed up of two tables in the order of name that is order by name.
select slug, name, 1 as mt
from tablea
union
select slug, name, 0 as mt
from tableb
order
by name;
The above is working well for me. will there be any complications in the result of this?
Suppose your query is
SELECT field1 FROM TABLE1 WHERE 1
UNION SELECT field1 FROM TABLE2 WHERE 1
u can make it a subquery like this
SELECT * FROM (SELECT field1 FROM TABLE1 WHERE 1
UNION SELECT field1 FROM TABLE2 WHERE 1) AS `result` ORDER BY `result`.`field1`
Or, you could use a Join query such as:
SELECT tablea.firstname, tablea.middlename, tablea.lastname, tableb.phone
FROM tablea, tableb
WHERE tablea.ID = tableb.ID
Then, you could sort the result however you like.