473,406 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

query on an array in Mysql

I've a table on mysql:

code1 tinyint, code2 tinyint, notcode1 varchar(20),notcode2
varchar(20),label

I do have some records:
values(1,null,null,null,'text1');
values(2,null,null,'17,18','text2');
values(2,null,null,'15','text3');

look at the second record
in my case if code1 = 2 and (code2 <> 17 and code2 <> 18) it should return
'text2'.

But how to create a query since I've values for code1 and code2 ?

where $valcode1 = code1 and ($valcode2 not in notcode2) ?
but how to do not in notcode2 in mysql ?
I've full control on the table, so I may put any kind of field type (didnt'
find any array type in mysql and since enum and set are predefined values,
they seems not fit my need since the values are different from case to case.
but I may be wrong since I don't know how to use enum and set fields)

Of course, if the values are 2 and 17, text3 must be in the query result !

Bob

PS: I've no access to mysql NG with my ISP, so please don't tell me not to
ask in this NG.
Jul 17 '05 #1
6 1727
Bob Bedford wrote:
I've a table on mysql:

code1 tinyint, code2 tinyint, notcode1 varchar(20),notcode2
varchar(20),label

I do have some records:
values(1,null,null,null,'text1');
values(2,null,null,'17,18','text2');
values(2,null,null,'15','text3');


Doesn't seem like a sound table structure, and i don't see a unique index.

I would alter your table to add an index and forget about the notcode1
column, then create a second table with an index, and columns for the
index of your first table and a possible value.

That way, u can select the id from the first table, and use that to get
at the possible values for notcode1 in the second table.
HTH

Stijn
Jul 17 '05 #2

"Stijn Verholen" <st***@skynet.be> a écrit dans le message de news:
42*********************@news.skynet.be...
Bob Bedford wrote:
I've a table on mysql:

code1 tinyint, code2 tinyint, notcode1 varchar(20),notcode2
varchar(20),label

I do have some records:
values(1,null,null,null,'text1');
values(2,null,null,'17,18','text2');
values(2,null,null,'15','text3');


Doesn't seem like a sound table structure, and i don't see a unique index.

I would alter your table to add an index and forget about the notcode1
column, then create a second table with an index, and columns for the
index of your first table and a possible value.

That way, u can select the id from the first table, and use that to get at
the possible values for notcode1 in the second table.


Thanks for your advice.
Actually this i a part of a very big query. but it takes too much time as I
do "case" and "if" in the query.
I must keep the notcode1, as the same code can be on the notcode2 and I've
to differenciate the 2 fields.
I've created the notcode table (id,code) so here are my records:
(code1,code2,notcode1,notcode2,label)
insert into label values(1,null,null,null,'text1');
insert into label values(2,null,null,1,'text2');
insert into label values(null,18,null,null,'text3');
insert into label values(null,17,null,null,'text4');
insert into label values(3,null,null,null,'text5');
insert into label values(4,null,null,null,'text6');
insert into label values(5,null,null,null,'text5'); //yes also text5

notcode
(id,code)
insert into notcode values(1,17);
insert into notcode values(1,18);

my test values (table a).
code1,code2 (for simulating what comes from a very big query)
insert into a values(1,0); //should return text1
insert into a values(2,9); //should return text2
insert into a values(3,8); //should return text5
insert into a values(7,9); //should return nothing
insert into a values(10,17); //should return text4

Anyway ,what would be the query ?

I've tried this
SELECT *
FROM a,
label left join notcode on label.notcode2 = notcode.id
WHERE a.code1 = label.code1
OR a.code2 = label.code2
AND (label.notcode2 is null or label.code2 <> notcode.code)

this gives me:
text1
text 2
text 2 //again, but looking at the result, it returns one for 17 and one for
18 as it's linked to notcode
text 5
text 4

This seems to work, but how to avoid the 2 times text 2 (it should return
only 1 result)? I absolutely need only 1 result.

Bob
Jul 17 '05 #3
Bob Bedford wrote:

"Stijn Verholen" <st***@skynet.be> a écrit dans le message de news:
42*********************@news.skynet.be...
Bob Bedford wrote:
I've a table on mysql:

code1 tinyint, code2 tinyint, notcode1 varchar(20),notcode2
varchar(20),label

I do have some records:
values(1,null,null,null,'text1');
values(2,null,null,'17,18','text2');
values(2,null,null,'15','text3');

Doesn't seem like a sound table structure, and i don't see a unique
index.

I would alter your table to add an index and forget about the notcode1
column, then create a second table with an index, and columns for the
index of your first table and a possible value.

That way, u can select the id from the first table, and use that to
get at the possible values for notcode1 in the second table.

Thanks for your advice.
Actually this i a part of a very big query. but it takes too much time
as I do "case" and "if" in the query.
I must keep the notcode1, as the same code can be on the notcode2 and
I've to differenciate the 2 fields.
I've created the notcode table (id,code) so here are my records:
(code1,code2,notcode1,notcode2,label)
insert into label values(1,null,null,null,'text1');
insert into label values(2,null,null,1,'text2');
insert into label values(null,18,null,null,'text3');
insert into label values(null,17,null,null,'text4');
insert into label values(3,null,null,null,'text5');
insert into label values(4,null,null,null,'text6');
insert into label values(5,null,null,null,'text5'); //yes also text5

notcode
(id,code)
insert into notcode values(1,17);
insert into notcode values(1,18);

my test values (table a).
code1,code2 (for simulating what comes from a very big query)
insert into a values(1,0); //should return text1
insert into a values(2,9); //should return text2
insert into a values(3,8); //should return text5
insert into a values(7,9); //should return nothing
insert into a values(10,17); //should return text4

Anyway ,what would be the query ?

I've tried this
SELECT *
FROM a,
label left join notcode on label.notcode2 = notcode.id
WHERE a.code1 = label.code1
OR a.code2 = label.code2
AND (label.notcode2 is null or label.code2 <> notcode.code)

this gives me:
text1
text 2
text 2 //again, but looking at the result, it returns one for 17 and one
for 18 as it's linked to notcode
text 5
text 4

This seems to work, but how to avoid the 2 times text 2 (it should
return only 1 result)? I absolutely need only 1 result.

Bob

If all you are after is the label change the * after SELECT to
DISTINCT a.label in order to eliminate the duplicates.

HTH
Jerry
Jul 17 '05 #4
Bob Bedford wrote:
I've tried this
SELECT *
FROM a,
label left join notcode on label.notcode2 = notcode.id
WHERE a.code1 = label.code1
OR a.code2 = label.code2
AND (label.notcode2 is null or label.code2 <> notcode.code)

[snip]

This seems to work, but how to avoid the 2 times text 2 (it should
return only 1 result)? I absolutely need only 1 result.

Bob


I still isn't quite clear to me how your database is structured and even
what it is exactly u are trying to accomplish. I have a gut feeling
there are better ways to do what u are doing.

That said, your query needs a subquery. Try this:

SELECT * FROM a, label, notcode
WHERE (a.code1 = label.code1 OR a.code2 = label.code2) AND
(label.notcode2 IS NULL OR
label.code2 NOT IN (SELECT code FROM notcode WHERE id = label.notcode2));

I have not tested this btw.
Stijn
Jul 17 '05 #5
> That said, your query needs a subquery. Try this:

SELECT * FROM a, label, notcode
WHERE (a.code1 = label.code1 OR a.code2 = label.code2) AND
(label.notcode2 IS NULL OR
label.code2 NOT IN (SELECT code FROM notcode WHERE id = label.notcode2));

I have not tested this btw.


I'ts what I'm trying to do since the beginning, but the query fails at mysql
4 doesn't seem to allow sub-queries
Jul 17 '05 #6
Bob Bedford wrote:
That said, your query needs a subquery. Try this:

SELECT * FROM a, label, notcode
WHERE (a.code1 = label.code1 OR a.code2 = label.code2) AND
(label.notcode2 IS NULL OR
label.code2 NOT IN (SELECT code FROM notcode WHERE id = label.notcode2));

I have not tested this btw.

I'ts what I'm trying to do since the beginning, but the query fails at
mysql 4 doesn't seem to allow sub-queries


You do indeed need MySQL 4.1 or higher for that.

Take a look here for how to rewrite that subquery:
http://dev.mysql.com/doc/mysql/en/re...ubqueries.html
Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: davidv | last post by:
I am building a very simple form that takes text and inserts data in a MySQL db. I would like my "logic" to simply insert the value in to the field in the database that matches the name from the...
3
by: 'bonehead | last post by:
Greetings, I'd like to figure out some syntax for retrieving the data from a table when I don't know all the of field names. What I do know are, the name of the table, the names of the primary...
9
by: Börni | last post by:
Hi, I have an sql query like this: SELECT column FROM table WHERE column1="3" AND column2="1" This query works perfectly if i run it in the command line, to be exactly it return two results. But...
4
by: Diamondback | last post by:
I have two tables, WIDGETS and VERSIONS. The WIDGETS table has descriptive information about the widgets while the VERSIONS table contains IDs relating to different iterations of those widgets...
1
by: Jaime Leivers | last post by:
Here's a median function in access that you can call in any query. You could change this to any excel function you wanted. Most people can find the windows help file that says how to call an excel...
7
by: Daz | last post by:
Hi. I am trying to select data from two separate MySQL tables, where I cannot use join, but when I put the two select queries into a single query, I get an error telling me to check my syntax. Both...
2
by: Flic | last post by:
Hi, I have a basic db that I access with MySQL query browser. Everything seems fine to me but I am using this db as part of a php shopping basket and when I try to add an item I get: Notice:...
4
by: TechnoAtif | last post by:
Hi ALL I have entered some array values using checkboxes into mysql database through a form. Next iam creating a searchpage where all those cateogories inserted through checkboxes has to be...
4
by: zion4ever | last post by:
Hello good people, Please bear with me as this is my first post and I am relative new to ASP. I do have VB6 experience. I have a form which enables users within our company to do an intranet...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.