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'
);
Related
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)
How to:
Get values from table1.column1 (e.g. abc)
table1.column1=abc
Concatenate them with certain fixed strings, e.g.
xxx
yyy
zzz
Insert the results as separate rows in table2.column2. The final result should be rows with values like this:
table2.column2=abc_xxx
table2.column2=abc_yyy
table2.column2=abc_zzz
(table2 has a connecting column indicating to which ID the table2.column2 record corresponds in case this matters)
Repeat this process for all records in table1.column1 which have table1.item_id > 100
EDIT: For certain convenience I would like the final result rows sequence to look like:
source1_xxx
source1_yyy
source1_zzz
source2_xxx
source2_yyy
source2_zzz
and not like:
source1_xxx
source2_xxx
source1_yyy
source2_yyy
source1_zzz
source2_zzz
If I understand you correctly, you want N (e.g. 3) entries for every existing row in Table1. If so, you can CROSS JOIN Table1 to a projection of the values, like so:
INSERT INTO Table2(column2)
SELECT CONCAT(t1.column1, '_', x.col)
FROM Table1 t1
CROSS JOIN
(SELECT 'xxx' AS col
UNION
SELECT 'yyy'
UNION
SELECT 'zzz') x;
SqlFiddle here
Edit
The query was updated to be cognaisant of the ordering and filtering requirements as well:
INSERT INTO Table2(column2)
SELECT CONCAT(t1.column1, '_', x.col)
FROM Table1 t1
CROSS JOIN
(SELECT 'xxx' AS col
UNION
SELECT 'yyy'
UNION
SELECT 'zzz') x
WHERE t1.ID > 100
ORDER BY t1.column1 ASC, x.col ASC;
With an updated SqlFiddle
Modifying the answer.
Credit goes to the StuartLC ..he is right, you would need to use cross join
INSERT INTO Table2(column2)
SELECT CONCAT(t1.column1, '_', x.col)
FROM Table1 t1
CROSS JOIN
(SELECT 'xxx' AS col
UNION
SELECT 'yyy'
UNION
SELECT 'zzz') x;
is this what u want
insert into table2(column2)
select concat(col1,'_','xxx') from table1
union
select concat(1col1,'_','yyy') from table1
union
select concat(col1,'_','zzz') from table1
else keep this entire select statemnts in aview and use it in the insert statement
create view abc
as
select concat(col1,'_','xxx') from table1
union
select concat(1col1,'_','yyy') from table1
union
select concat(col1,'_','zzz') from table1
then
insert into table2(column2) select * from abc
the case is that I need to select a field distinct from table1 (no duplicates) and use the result as a key to select from another table2. And I need this to be in one query. Is this possible?!
table1: hID, hName, hLocation
table2: hID, hFrom, hTo, hRate, hRoomType, hMeals
I want to correct version of this query:
SELECT
*
FROM
table1
JOIN (
DISTINCT
hID
FROM
table2
WHERE
hRoomType = Double Room
ON table1.hID = table2.hID)
expected result: all hotels that offer Double Room thanks much –
thanks for help!
Your question is quite vague and confusing. Is this what you are looking for:
SELECT hID, name, location
FROM table2
INNER JOIN table1
ON table1.hID = table2.hID
GROUP BY table2.hID;
Here is a skeleton to achieve this:
SELECT
* -- Don't forget to list the requested fields instead of using `*`!
FROM (
-- This is the distinct list from table1
SELECT DISTINCT
id
FROM
table1 T1
) DT1
INNER JOIN table2 T2
ON T1.id = T2.reference_to_t1_id
Another solution if you don't want to retrieve any columns from table1:
SELECT
* -- Don't forget to list the requested fields instead of using `*`!
FROM
table2 T2
WHERE
-- Sais that get all record from table2 where this condition matches
-- at least one record
EXISTS (
SELECT 1 FROM table1 T1 WHERE T1.id = T2.reference_to_t1_id
)
For your tables and question
SELECT
hID, hName, hLocation
FROM
table1 T1
WHERE
EXISTS (
SELECT 1 FROM
table2 T2
WHERE
T1.hID = T2.hID
AND T.hRoomType = 'Double' -- Assuming that this is the definition of double rooms
)
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
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)