I have a table which contains strings which is needed to be searched [like below image]
TableA
After that, I have a table which stores data [like below image]
TableB
And what the search function does is to select from TableB only if the record contains the string in TableA [i.e. The expected result should be like below image(TableC)]
TableC
I've tried using the SQL below, but the SQL had some error while trying to run [Incorrect syntax near 'select'] , also, the SQL is a bit complicated, is there any way to make the SQL simplier?
select * from TableB a
where exists
(select colA from TableB b
where b.colA = a.ColA and Contains (b.ColA, select searchCol from TableA))
Please try:
select
a.*
From tblA a inner join tblB b
on b.Col like '%'+a.Col+'%'
SQL Fiddle Demo
One of these should work according to me
SELECT b.colA
FROM TableB b join TableA a
WHERE colA like '%' + a.ColA + '%';
SELECT b.colA
FROM TableB b join TableA a
WHERE INSTR(b.colA, '{' + a.colA + '}') > 0;
SELECT b.* FROM tblA a,tblB b
WHERE PATINDEX('%'+a.Col+'%',b.Col)>0
SQL FIDDLE DEMO
I have searched for performance difference between PATINDEX and LIKE but not got a conclusive answer , some say PATINDEX is faster when indexing can't be used which is when pattern to be searched is enclosed within wildcard character eg '%'+a.Col+'%' while other post mention they can similar performance .
Perhaps some SO SQL Wizard can look in crystal ball and show us the light
Related
I have a stored procedure for a select query and there are left join and inner join commands in the query. I'm using the script below for dependencies of the stored procedure.
select
referenced_database_name,
referenced_entity_name,
referenced_minor_name
from
sys.dm_sql_referenced_entities ('dbo.sp_test', 'OBJECT')
How can I get the column name which used for joining tables in this SP? Or is there any simple way to get/parse column names from this script:
select column1, column2
from tableA
inner join tableB on tableB.id = tableA.id
I need tableB.id and tableA.id keywords from the script
I've tried parse that script with substring, left etc functions but they're insecure for me. There are lots of piece of select query using union all.
I'v solved my problem parsing the query. At first, i left a delimiter (--*) for splitting the section which contains joins then used that code for parsing the query (i know its not professional but it has worked for me) :
select distinct
referenced_database_name,
referenced_entity_name,
referenced_minor_name,
SUBSTRING(c.value,CHARINDEX('on',value)+3,len(value)) split
,IIF(SUBSTRING(c.value,CHARINDEX('on',value)+3,len(value)) like '%'+CONCAT(referenced_entity_name,'.',referenced_minor_name)+'%',1,0) as clm
from sys.dm_sql_referenced_entities ('dbo.sp_test', 'OBJECT') a
left join sys.sysdepends b on OBJECT_NAME(depid) = a.referenced_entity_name
outer apply string_split(REPLACE(OBJECT_DEFINITION(id),'--*','/'),'/') c
where OBJECT_NAME(id) = 'sp_test' and c.value like '%join%' and referenced_minor_name is not null
i want to join two tables in hive based on the partial match. So far, i tried with the below SQL query:
select * from tableA a join tableB b on a.id like '%'+b.id+'%';
and instr but nothing working, is there a way?
JOIN in Hive only support equation conditions. Plus, you should use CONCAT for your string concatenation.
This is one solution instead.
select *
from tableA a, tableB b
where a.id like concat('%',b.id,'%')
I am using SQL Server 2008 and I have two tables that I want to join. I have provided something below that shows how my data looks. I want to join the two tables on the given columns, but how can I do this with the "ID" in front of the number in table B? I was thinking of a trim on the join, but I don't know how to do that.
Something like...
Select *
From TableA AS A
Left Join TableB AS B
On A.ColumnA = B.ColumnB
But this won't work because the numbers don't completely match up.
TableA ColumnA
123
456
789
TableB ColumnB
ID123
ID456
ID789
I hope I made this clear enough. Any suggestions?
SQL Fiddle Demo
select *
from tableA a
join tableB b
on 'ID' + cast(columnA as varchar(5)) = b.columnB
Hello I Have a problem in getting rows from one table after comparing both. Detail of Both Table are as follows:-
I am using Ms Access database.
TableA is having a data of numeric type (Field Name is A it is primary key)
----------
Field A
==========
1
2
3
4
5
Table B is having data of numeric type ( Field Name is A it is foreign key)
--------
Field A
========
2
4
Now I am using below query which is this
select a.a
from a a
, b b
where a.a <> b.b
I want to show all the data from Table A which is not equal to Table B. But the above query is not working as I described.
Can you help me in this regard.
Regards,
Fawad Munir
In an attempt at clarity, I've used upper case for tables and lower case for fields:
Select A.a
FROM A LEFT OUTER JOIN B ON A.a=B.b
WHERE B.b is null
This will show all the records in A that are not in B (I assume that's what you want).
Read up on Access outer joins. In the query designer you double click the join and select something like "all records from table a and only the matching records in table b".
In your question you said that the name of the field in table B is 'A'. Given that, I'd say that your query should be something like
select a.a
from a, b
where a.a <> b.a
But I'm not sure this will do what you want. I think you're trying to find rows in table A which do not have a matching row in table B, in which case you might try
SELECT A.A
FROM A
LEFT OUTER JOIN B
ON (B.A = A.A)
WHERE B.A IS NULL
Try that and see if it does what you want.
Share and enjoy.
I don't know exactly if Access would accept the syntax, but here how I would do in SQL Server.
select a.a
from TableA a
where a.a NOT IN (
select b.a
from TableB b
)
or even as above-mentioned:
select a.a
from TableA a
left outer join TableB b on b.a = a.a
where b.a IS NULL
Its not entirely clear what you are trying to achieve, but its sounds like you are attempting to solve the common problem of finding rows in Table A missing associated data in Table B. If this is the case, it appears you misunderstand the semantics of the join you tried. In which case, you have 2 problems, because the understanding the the JOIN operation is critical to working with relational databases.
In relation to the first problem, please research how to express a subquery using the IN operator. Something like
... WHERE a NOT IN (SELECT a from b)
In relation to the second problem, try your query without the WHERE restriction, and see what is returned. Once you understand what the join is doing, you will see why applying a WHERE restriction to it will not solve your problem.
If I understand you correctly, you want to see every row in A for which column a contains a value that cannot be found in any column b value of B. You can get this data in several ways.
I think using NOT IN is the clearest, personally:
SELECT * FROM tableA WHERE columnA NOT IN
(SELECT columnB FROM tableB WHERE columnB IS NOT NULL)
Many people prefer a filtered JOIN:
SELECT tableA.* FROM tableA LEFT OUTER JOIN tableB
ON tableA.columnA = tableB.columnB WHERE tableB.columnB IS NULL
There is a NOT EXISTS variant as well:
SELECT * FROM tableA WHERE columnA NOT EXISTS
(SELECT * FROM tableB WHERE columnB = tableA.columnA)
I have tried a bunch of different things but always get syntax errors.
I have two tables - tableA and tableB. They both have a con_number field as a unique ID.
I want to select all of the rows in tableB that do not exist in tableA.
Can anyone please give me this query as it would be in MS Access?
I know that using NOT IN is quite inefficient in this case so if there is a better way then that would be great.
Thanks.
SELECT TableB.con_number
FROM TableB
WHERE NOT EXISTS (SELECT 1
FROM TableA
WHERE TableA.con_number = TableB.con_number);
NOT IN version (slow but sure):
SELECT con_number
FROM TableB
WHERE con_number NOT IN (SELECT con_number FROM tableA);
experimental version (don't know if this is any faster, just try it out):
SELECT B.con_number, MAX(A.con_number) AS check
FROM tableB B LEFT JOIN tableA A ON B.con_number = A.con_number
GROUP BY B.con_number
HAVING check IS NULL;
Note: Both should be fairly standard SQL, I don't know any ms-access specific features
There is a Find Unmatched wizard that will set this up. The SQL is:
SELECT TableB.con_number
FROM TableB LEFT JOIN TableA
ON TableB.con_number = TableA.con_number
WHERE TableA.con_number Is Null
I remember something like this one:
SELECT * FROM TableA.* LEFT JOIN TableB _
ON TableA.con_number = TableB.con_number WHERE 'criteria'
But I don't remember which 'criteria' to use
... TableA.con_number <> TableB.con_Number
... TableB.con_number IS NULL
... TableA.con_number NOT like TableB.con_Number