How to run a case sensative comparison on text fields Access - ms-access

I am trying to run a query to remove a set of ID's from a table when they are present in a field from another table.
The problem is both ID fields are of type text and the search does not appear to be case sensitive (but I need it to be). (i.e. ABC123 is different than abc123)
I am running a query similar to Select myID from table1 where myID NOT IN (Select otherID from table2)
What modification do I need to make in my Access query to make the results case sensitive when running comparison?

Try this:
SELECT a.*
FROM table1 a LEFT JOIN table2 b
ON a.myID = b.otherID
WHERE StrComp(IIF(IsNull(b.otherID ), a.myID , b.otherID), a.myID, 0) <> 0
OR IsNull(b.otherID)

Related

why using IN (or NOT IN) clause in a query makes it really slow

I have a query:
SELECT DISTINCT field1 FROM table1 WHERE field2 = something
(table1 contains 1 million records, execution time:0.106sec, returns: 20 records)
Another query
SELECT DISTINCT similarField1 FROM table2 WHERE similarField2 = somethingElse
(table2 contains half million records, execution time:0.078sec, returns: 20 records)
Now if I run a query, by combining above both:
SELECT DISTINCT field1 FROM table1 WHERE field2 = something AND field1 NOT IN (SELECT DISTINCT similarField1 FROM table2 WHERE similarField2 = somethingElse)
It does't give result even running for 10mins. Why it has became dramatically slow, and what could be a potential solution.
edit: I am using MySQL with dbvisualizer 6.5
You don't need to use DISTINCT on the sub-query. Try to use NOT EXISTS which probably is more efficient in SQL-Server:
SELECT DISTINCT field1
FROM table1
WHERE field2 = #something
AND NOT EXISTS
(
SELECT 1 FROM table2
WHERE table2.similarfield1 = table1.field2
AND table2.similarfield2 = #somethingelse
)
Edit: Since you have updated the tags, i'm not sure if this is more efficient in MySql. However, i'd prefer NOT EXISTS anyway since it also works with NULL values(if you use IS NULL) and is easier to read and to maintain.
my query and advice are similar to #TimSchmelter.
In fact you should not use distinct at all. First you should remove distinct and check if you are getting duplicate records you have just ask part of your problem.Table design are not clear.
You should post your complete problem and query here without any hesitant. Also don't forget to apply index on feild2, feild1,similarField1,similarField2.
SELECT DISTINCT field1
FROM table1 tbl1
WHERE field2 = something
AND NOT EXISTS (
SELECT similarField1
FROM table2 tbl2
WHERE tbl1.field1 = tbl2.similarField1
AND similarField2 = somethingElse
)

Get a list of ids not present in a table

I have a list of ids, and I want to query a mysql table for ids not present in the table.
e.g.
list_of_ids = [1,2,4]
mysql table
id
1
3
5
6
..
Query should return [2,4] because those are the ids not in the table
since we cant view ur code i can only work on asumption
Try this anyway
SELECT id FROM list_of_ids
WHERE id NOT IN (SELECT id
FROM table)
I hope this helps
There is a horrible text-based hack:
SELECT
substr(result,2,length(result)-2) AS notmatched
FROM (
SELECT
#set:=replace(#set,concat(',',id,','),',') AS result
FROM (
select #set:=concat(',',
'1,2,4' -- your list here
,',')
) AS setinit,
tablename --Your tablename here
) AS innerview
ORDER BY LENGTH(result)
LIMIT 1;
If you represent your ids as a derived table, then you can do this directly in SQL:
select list.val
from (select 1 as val union all
select 2 union all
select 4
) list left outer join
t
on t.id = list.val
where t.id is null;
SQL doesn't really have a "list" type, so your question is ambiguous. If you mean a comma separated string, then a text hack might work. If you mean a table, then something like this might work. If you are constructing the SQL statement, I would advise you to go down this route, because it should be more efficient.

Update Query using SELECT results

As my application is expanding, I now am changing the structure of my database; I now want to control file types within the database. I wanted to start with the current file types already in the database. My Database now has a [simplified] 2 table structure like:
tbFiles: pkFileID, fileType, fileName
tblFileType: pkFileType, typeName, typeDesc
I am trying to have the output of a SELECT query update into the newly created tblFileType table. I have tried among other things:
UPDATE tblFileType
INNER JOIN
(SELECT DISTINCT fileType FROM tblFiles) as X
SET typeName = fileType
but I always seem to get 0 row(s) affected.
When I run
SELECT DISTINCT fileType
FROM `tblFiles`
I get Showing rows 0 - 22 (~23 total, Query took 0.0074 sec)
I know this must be simple, but why is the UPDATE query not affecting 23 rows?
You need to add a JOIN condition like ON t1.fileType = x.fileType as follows:
UPDATE tblFileType t1
INNER JOIN
(
SELECT DISTINCT fileType
FROM tblFiles
)as X ON t1.fileType = x.fileType
SET t1.typeName = X.fileType
Update: Since the table tblFileType is blank, you will need to use INSERT something like:
INSERT INTO tblFileType(typeName )
SELECT DISTINCT fileType
FROM tblFiles
WHERE -- a condition here
you just want to populate the table - not update anything in there (especially since nothing exists yet)
INSERT INTO tblFileType(typeName )
SELECT DISTINCT fileType FROM tblFiles

selecting data from 2 tables where multiple columns and rows intersect with 'X'

its tricky to explain what i am trying to do so here goes
I have 2 tables derived from a single excel spreadsheet. the spreadsheet defines causes and effects.It has 3 points of reference so i cant make it into 1 table
this spreadsheet has a list of events down the left and effects accross the top. these are associated with each other by inserting an X in the row/column matching the event to the effect. So the intersecting columns/rows form a matrix. So for eg an event could be. 'turn on lightswitch'. follow this row along untill 'X' is found. then follow column up to effects and it shows 'light turns on' (its a little more complicated than that in reality) there can be multiple effects defined by X's in the same row
My idea was in the where statement to use table1.* and table2.* but I was looking for a wildcard to search rows/columns but after research this is not possible in mysql. Using OR for all the furter row/column combinations wont work because it just shows everything with an X in
I am a bit stumped how to query the X row/column part of the table so only the 'example' result part of the query is displayed after searching all the X's A single query below is successful but i need to search table1.1-60 table2.2-61 for occurrances of 'X' by only changeing the '%example%' part of the query in a webpage mysql/php type form and then displaying the results on another page
any suggestions/alternatives welcome thanks
SELECT
table1.equipment,
table1.tagno,
table2.equipment,
table2.action,
table2.service,
table2.tagno
FROM
table1 ,
table2
WHERE
table1.tagno LIKE '%example%' AND
table1.1 = 'X' AND
table2.2 = 'X'
There is no natural way to join column N from table1 with column N+1 in table2; you'll have to specify the joins "by hand" (or in a program, both of which will be ugly).
You want:
select * from table1 t1, table2 t2
where t1.tagno like '%example%' AND t1.1 ='X' and t2.2 = 'X'
UNION
select * from table1 t1, table2 t2
where t1.tagno like '%example%' AND t1.2 ='X' and t2.3 = 'X'
UNION
-- ...
This could be very inefficient. As suggested in the comments, remodelling the data would give you cleaner queries and a happier future.
To give more insight into my suggestion, I would re-model the data like so:
CREATE TABLE effects (
id INT,
name VARCHAR(100)
);
CREATE TABLE events (
id INT,
name VARCHAR(100)
);
CREATE TABLE effects_events (
effect_id INT,
event_id INT
);
effects_events is a Junction Table
So you could query it like this:
SELECT * FROM events WHERE events.id = effects_events.event_id AND effects_events.effect_id = effects.id AND effects.name = 'light turns on';
Or:
SELECT * FROM effects WHERE effects.id = effects_events.effect_id AND effects_events.event_id = events.id AND events.name = 'turn on lightswitch';
I might be misunderstanding something in your question but this seems like the most straightforward solution to me.

mysql query with when statement comparing

I want to write mysql when statement and i can't do it.
explanation => localParty is column in table "data", loc is column from table "o", and i want to compare localParty to loc, if their values are equal then i want to retrieve information from loc_m column (this column is from table "o"), and if not equal then from localParty column (from "data" table)
Please help how to write this script in mysql query ? Thanks
with this script
select (case when data.localparty = o.loc then o.loc_m else data.localparty end)
as customdata from data, o
it is working but it is missing exactly three result ( I mean that then data.localparty equal to o.loca it is giving result from data.localparty 3 times and after it one time it is giving result from loc_m and it is going like so .
You could modify the query in the following way:
SELECT IF(t1.Column1 = t2.Column2,t2.Column1,t1.Column3) FROM TABLE1 AS t1, Table2 AS t2
Try This:
select (case when data.localparty = o.loc then o.loc_m else data.localparty end)
as customdata from data, o
you can use following query
Select O.loc_m as local
from Data
inner join on O on data.localparty=O.loc
UNION
Select data.loacalparty as local
from Data
where data.localparty is not in (select loc from O )
You should use control-flow functions to achieve that goal:
SELECT IF(Column1 = Column2,Column1,Column3) FROM TABLE1