How to write a SQL like query with table column ?
I have two tables table1 and table2.
Table1 has Notes
Table2 has billid
We have around 100 bills which satisfies Like condition.
Right now, i am writing with Static value on the column.
How should i write a Like Query by passing Column in the Like Condition.
select * from table1
where Notes like select billid from table2
You are wanting a JOIN here.
If the exact BillID is found in Table1.Column1 then:
select table1.*
from table1
INNER JOIN table2 ON
table1.column1=table2.billid
You could also do this using IN in the WHERE clause if you truly don't need any records from your table2 in the result set:
select *
from table1
where column1 in (SELECT billid FROM table2);
Which is pretty close to what you were aiming for in your attempted query.
Lastly, if you actually mean LIKE which is more of a wildcard match in SQL terms where the column1 merely has to contain a billid then back to the join:
select table1.*
from table1
INNER JOIN table2 ON
table1.column1 LIKE '%' + table2.billid + '%'
Try this. Select a raw from table1 if any match from table2 exists
select *
from table1
where exists (select billid
from table2
where table1.column1 LIKE '%' + table2.billid + '%')
Related
So, I have a table with the following structure:
id
columnA
columnB
1
Yes
1
No
I want to combine the row into a single row, so it ends up like this:
id
columnA
columnB
1
Yes
No
I believe a self join here would work like this:
SELECT t1.columnA , t2.columnB
FROM table1 t1, table1 t2
where t1.id = t2.id
But is there a way to do this without specifying the columns? I have a table that has 100 columns and I'm trying to see if I can accomplish this without listing out all the columns.
Use the below query to get the column name with aggregation (Query created using information schema to get the column names). Write a select using the result and run the query.
select
case when column_name='Id' then column_name
else concat(',Max(', column_name,')') end as Name
from information_schema.columns
where table_name = 'Table1';
You will get something like below as output, where A and B are the column names.
Id
,Max(A)
,Max(B)
Add convert the result to query
Select
Id
,Max(A)
,Max(B)
from Table1 Group by Id
is there a way to do this without specifying the columns?
You can use using to answer your question.
SELECT t1.columnA , t2.columnB
FROM table1 t1 JOIN
table1 t2
USING (id);
To get the data you want, use aggregation:
SELECT id, MAX(t1.columnA), MAX(t2.columnB)
FROM table1 t1
GROUP BY id;
Use your JOIN only change the colums for *
SELECT t1.*, t2.*
FROM table1 t1, table1 t2
where t1.id = t2.id
Heyy I'm new to sql and I'd just like to know if there's a way to retrieve select statements with conditions from other tables.
I want to select all name values that have a number that identifies that they have committed a crime. I only want to select a name once.
"SELECT distinct * FROM Table1 WHERE number LIKE table2.number "
Are you looking for IN?
SELECT t1.*
FROM Table1 t1
WHERE t1.number IN (SELECT t2.number FROM table2 t2 t2.number);
Under most circumstances, the rows in a table should be unique. So, you don't need SELECT DISTINCT. The DISTINCT can add a considerable amount of overhead to such a query.
You can able to use INNER JOIN like below,
select tbl1.Name from tableOne tbl1
inner join tableTwo tbl2 ON tbl1.commonKey = tbl2.commonKey
where tbl1.columnName = 'any value'
SO I've searched high and low, trying other tips used on this forum to no avail.
So trying to delete using inner join in Oracle SQL Developer (v3.2.20.09)
Table I wish to delete from (Table1, column name Column1), where the data matches the column 'Column2' in 'Table2.
I know there are some differences between Oracle/Microsoft SQL, tried multiple queries such as below, with slight variation (using open/close brackets, inner joins, WHERE EXISTS, WHERE (select's).
TRY:
delete from table2 where
exists (select column1 from table1);
delete from table2,
inner join table1 on table2.column2 = table1.column1;
What are the problem(s) of the code that I wrote?
The EXISTS version would look like this:
delete from table2
where exists (select *
from table1
where table1.column1 = table2.column2);
Alternatively you can use an IN clause
delete from table2
where column2 in (select column1
from table1);
If you're trying to delete from table1, then that's the table name that has to be used in the delete clause, not table2.
delete table1 t1
where exists (select null
from table2 t2
where t2.column2 = t1.column1)
How to make query like this:
SELECT column1 FROM table1 WHERE ppp=$parameter
AND (SELECT * FROM table2 WHERE parameter=$parameter AND qweqwe=$parameter2)
PS: '$' not from PHP, I just show that this values are variable.
Actually, I need AND qweqwe<650 in the second query
Maybe something like this:
SELECT
column1
FROM
table1
WHERE
ppp=$parameter
AND EXISTS
(
SELECT
NULL
FROM
table2
WHERE
table1.ppp=table2.parameter
AND qweqwe<$parameter2
)
SELECT column1 FROM table1 WHERE parameter=$parameter
AND exists
(SELECT 1 FROM table2 WHERE table2.parameter=table1.parameter AND table2.qweqwe=$parameter2)
It depends on you data.
You can either use exists as shown in previous answers or use join.
SELECT column1 FROM table1
join table2 on table1.ppp=table2.parameter and table2.qweqwe=$parameter2
where table1.ppp=$parameter
In most cases join will be much faster then exists. But it can turn out that you have to use distinct if there's more then one recotd in table2 for each record in table1
what exactly are you trying to do?
the thing after first AND should be logical, for example
... AND 650<(SELECT qweqwe FROM table2 WHERE parameter=$parameter2)
Working in MS SQL 2005 and I want to use a select statement within a wildcard where clause like so:
SELECT text
FROM table_1
WHERE ID LIKE '%SELECT ID FROM table_2%'
I'm looking for product ids within a large body of text that is held in a DB. The SELECT statement in the wildcard clause will return 50+ rows. The statement above is obviously not the way to go. Any suggestions?
You can do a join and construct the like string based on table_2.
SELECT * FROM table_1 t1
INNER JOIN table_2 t2 ON t1.ID LIKE '%' + CONVERT(VARCHAR, t2.ID) + '%'