Wednesday, March 28, 2012

Query Problem in Access

hi friends i need help in this sql query

i have table like,

id fid
__ _____
autonumber text

and i am storing values like

id fid
___________________________________
1 1,2,3,4,5

2 11,12,13,14,15

now to find values i am using query

sql = SELECT * FROM test12 WHERE `fid` LIKE ('%1%')

only problem in this query is it is selecting 1 and 11 and i require
only 1 as i am giving one in %1%

now from this group some one give me the answer of this query

select *
from test
where fid = '1' -- singleton
or fid like '1,%' -- beginning of line
or fid like '%,1,%' -- middle of line
or fid like '%,1' -- end of line

now this query is running perfectly in other database except msaccess
2000. can anyone solve this problem. this query is not giving any
answer. it checks all those records which are singleton but not middle
of line records. and it seems to be problem in access only not in
mysql. it is working perfectly in mysql but not in access and as access
is my database in application i have to use access and i am really
irritate when i find in help that either i can use ' * ' or ' % ' in
expression on any one side like '%,1' or '%1,' but not like middle of
line that i am using '%,1,%'

here is example of my problem

sample table:=

id fid
___________________________________
1 1,2,3,4,5

2 11,12,13,14,15

query like

select *
from test
where fid = '1' -- singleton
or fid like '1,%' -- beginning of line
or fid like '%,1,%' -- middle of line
or fid like '%,1' -- end of line

will result id=1 perfectly but when i search

select *
from test
where fid = '2' -- singleton
or fid like '2,%' -- beginning of line
or fid like '%,2,%' -- middle of line
or fid like '%,2' -- end of line

it will not give me no output. plz help me i dont know what is the
problem if anyone can solve this i will be really thankful.On 19 Oct 2006 00:57:20 -0700, hardik wrote:

Quote:

Originally Posted by

>hi friends i need help in this sql query
>
>i have table like,
>
>id fid
>__ _____
>autonumber text
>
>and i am storing values like
>
>id fid
>___________________________________
>1 1,2,3,4,5
>
>2 11,12,13,14,15
>
>now to find values i am using query
>
>sql = SELECT * FROM test12 WHERE `fid` LIKE ('%1%')


(snip)

Hi hardik,

You should really change this design. The fid column violates the
principle of first normal form. That makes many queries needlessly
complex and slow. A proper design would split the comma-delimited list
in fid into seperate rows:

id fid
1 1
1 2
1 3
1 1
1 1
2 11
2 12
2 13
2 14
2 15

Then, you'd just use SELECT * FROM better_table WHERE fid = '1'

(snip)

Quote:

Originally Posted by

>now from this group some one give me the answer of this query
>
>select *
>from test
>where fid = '1' -- singleton
>or fid like '1,%' -- beginning of line
>or fid like '%,1,%' -- middle of line
>or fid like '%,1' -- end of line


Works, but there is a shorter kludge possible:
SELECT * FROM test WHERE ',' + fld + ',' LIKE '%,1,%'

Quote:

Originally Posted by

>now this query is running perfectly in other database except msaccess
>2000.


Access doesn't use the ANSI standard wildcards for LIKE searches. In
Access, you have t replace the '%' character with '*'.

But the best solution is: fix the design!!

--
Hugo Kornelis, SQL Server MVP

No comments:

Post a Comment