I have a Query which returns comma separated integers
like :
select GROUP_CONCAT(ids) from table2
now I want to use that result in another query
like :
select * from table1 where column in (select GROUP_CONCAT(ids) from table2)
in this case it will consider only first value in IN clause.
I agree with #Alma that this can't be done with IN, you might be able to do it with FIND_IN_SET, but if you can do it with IN it's probably a better approach :
SELECT *
FROM table1
WHERE find_in_set(ids, (
SELECT GROUP_CONCAT(ids)
FROM table2
)) != 0;
sqlfiddle demo
Any special reason not using a join and using a sub query
select * from table1 t1
JOIN table2 t2 on (t2.column = t1.ids)
Related
I am not able to return values for below query. The aim of this is I need to return the values depending on id.
oid are in form 123, 123.456, 123.456.789 . So if the result of SELECT id FROM table1 where name = 'Test999'is 456. it should return 2 columns i.e., 123.456, 123.456.789 . I am trying to give a % symbol before the query seems like some syntax error. Tried giving double quotes still no progress.
SELECT * from table1 where oid like
%(SELECT id FROM table1 where name = 'Test999')
I could manage it by adding a percent symbol to the output column of the subquery
select *
from (select 'aa' q union all select 'bb') some_table
where some_table.q like (select '%a')
Thus, your query must look something like that
SELECT * from table1 where oid like
(SELECT '%'||id FROM table1 where name = 'Test999')
What you must be aware of is query will fail whenever subquery returns more than 1 row
UPD: I'm dumb. You just need to eliminate the subquery:
SELECT *
from table1 t1
join table1 t2
on t1.oid like concat('%', t2.id)
You can use exists:
SELECT t1.*
FROM table1 t1
WHERE EXISTS (SELECT 1
FROM table1 tt1
WHERE t1.oid LIKE CONCAT('%', tt1.id, '%') AND
tt1.name = 'Test999'
);
My scheme looks like:
I have two tables: table1, table2
table1 has one field: table1-name of type string.
table2 has one field: table2-name of type string.
I want to return all rows of table1-name that are NOT substring of any table2-name records. I did:
SELECT DISTINCT `table2-name`.`table2`
FROM `table2`, `table1`
WHERE `table2-name`.`table2` NOT IN (SELECT `table1-name` FROM `table1`)
LIMIT 100;
But this returns all table2-name that are not equal to table1-name. What I need is all table2-namethat are not sub-string of table2-name.
Example:
table1-name:aa.abc.com, bb.com, xyz.com
table2-name: abc.com, aaa.com, xyz.com
The query above will return:
abc.com
aaa.com
What I want to return is:
aaa.com
I do not want abc.com to be returned because it is a sub-string of aa.abc.com.
Can you correct my query?
Use NOT EXISTS for such conditions:
select *
from table1 t1
where not exists
(
select *
from table2 t2
where t2.`table2-name` like concat('%', t1.`table1-name`, '%')
);
BTW: You should avoid names like table2-name where you need quotes in order to use them. Use something like table2_name instead.
If table2 is not very big, I wonder how this would perform:
select t1.*
from table1 t1 cross join
(select group_concat(t2.name separator '|') as names
from table2 t2
) t2
where t1.name not regexp t2.names;
Or, equivalently:
select t1.*
from table1 t1 cross join
(select group_concat(t2.name) as names
from table2 t2
) t2
where find_in_set(t1.name, t2.names) = 0;
This assumes that t2.name does not have special characters (for regexp) or commas (for find_in_set()).
To check if something is a substring of something else, you would need to use either 'LIKE' (as shown below) or possibly 'REGEXP_LIKE'.
SELECT DISTINCT table2.*
FROM table2
WHERE NOT EXISTS (SELECT 1 FROM table1 where table1.table1name LIKE CONCAT('%',table2.table2name,'%') > 0)
LIMIT 100;
On another note
1 - your problem description is not consistent (you confuse table1 and table2 repeatedly), choosing a better name would likely help with that.
2 - as you will see, I've taken the liberty of renaming the columns to drop the '-' character.
Another version that will work:
SELECT DISTINCT table2.*
FROM table2
WHERE table2name NOT IN (SELECT table2.table2name FROM table1 where table1.table1name like CONCAT('%',table2.table2name,'%') > 0)
LIMIT 100;
Please check the SQLFiddle (I also copied Thorsten Kettner's version above in there after renaming tables/columns)
I'm trying to search for all entries in one table where they have a column that matches entries in another column that precede a -
Example Output:
This is the query I tried, it returned the error of "Error in query (1242): Subquery returns more than 1 row"
SELECT * FROM table1
WHERE
table1.Column1 = (
SELECT
SUBSTRING_INDEX(table2.Column1,'-',1)
FROM
table2
WHERE
table2.column1 LIKE '%\-%'
);
You can use IN in your WHERE clause :
SELECT * FROM table1
WHERE
table1.Column1 IN (
SELECT
SUBSTRING_INDEX(table2.Column1,'-',1)
FROM
table2
WHERE
table2.column1 LIKE '%\-%'
);
Another way is to use JOIN as
SELECT * FROM table1 t1
inner join (
SELECT
SUBSTRING_INDEX(table2.Column1,'-',1) as str
FROM
table2
WHERE
table2.column1 LIKE '%\-%'
)t2
on
t1.column1 = t2.str
;
DEMO
im using the IN() function to match againts some ids.
SELECT * FROM my_table WHERE id IN(id1,id2,id3)
the thing is, now i need to calculate a SUM() based on the matched id, and i would like to do it on the same query. Something like
SELECT *,(SELECT SUM() WHERE id = the_matched_id) FROM my_table WHERE id IN(id1,id2,id3)
¿is it possible? maybe i should consider to change my query, or do it separately. ¿ What a do you suggest?
Thanks!
The matched ID is just the ID of each row from the outer table. You can use different aliases to compare these IDs.
SELECT *, (SELECT SUM(summableColName) FROM my_table t2 WHERE t2.id = t1.id)
FROM my_table t1 WHERE id IN (id1, id2, id3)
Try something like
SELECT id,sum(field) FROM my_table WHERE id IN(id1,id2,id3)
GROUP BY id
select * from tablename
where id in(select id from tablename2 where condition UNION select -1)
Is it ok to use select -1 as if the inner query does not result anything it will give error. It is feasible or not?
imho, inner-select is far from ideal (slow)
based on your posted SQL, an inner join will do the trick
select *
from tablename as t1
inner join tablename2 as t2
on t1.id=t2.id
where condition; --- your condition
If you have to get it done with a subquery then the correct way to do it would probably be:
SELECT *
FROM tablename AS t1
WHERE EXISTS
(SELECT id
FROM tablename2 AS t2
WHERE conditions)
It won't give an error if the query returns nothing. It just returns an empty resultset.