473,387 Members | 1,578 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,387 software developers and data experts.

select statement with multiple value where

I'm fairly new to creating complex sql statements and need a little
help.
In plain english I want to do this: Select menu rows where the row's
userGroupIDs equal the user's userGroupIDs.

Menu.userGroupID has multiple values seperated by commas.
User.userGroupID also has multiple values seperated by commas. I
realize some may want me to create seperate tables for these multiple
values. The reason I'm using comma seperation at the moment is because
I used a multi value <select> on a web page to collect and display the
choices from a single table.

Could I get an example of how to do this using a comma seperated field?

Many thanks.

Nov 8 '05 #1
3 6000
On 08/11/2005, rhamlin wrote:
I'm fairly new to creating complex sql statements and need a little
help.
In plain english I want to do this: Select menu rows where the row's
userGroupIDs equal the user's userGroupIDs.

Menu.userGroupID has multiple values seperated by commas.
User.userGroupID also has multiple values seperated by commas.
Ouch.
I realize some may want me to create seperate tables for these
multiple values.
Not some. Most.
Could I get an example of how to do this using a comma seperated
field?


See what (nonstandard) messy SQL you need to do this:

USE test;
DROP TABLE IF EXISTS users, menus, numbers;
CREATE TABLE users (
id INT NOT NULL PRIMARY KEY,
groupids CHAR(50)
);

INSERT INTO users VALUES
(1, '10,20,50');

CREATE TABLE menus (
id INT NOT NULL PRIMARY KEY,
groupids CHAR(50) NOT NULL
);

INSERT INTO menus VALUES
(1, '10,20'), (2, '10,30'), (3, '100,200'), (4, '50');
DROP TABLE IF EXISTS numbers;
CREATE TABLE numbers (
n TINYINT UNSIGNED NOT NULL PRIMARY KEY
);

INSERT INTO numbers VALUES (1), (2), (3), (4), (5);
-- as many as necessary
SELECT DISTINCT
menus.id
FROM users
JOIN numbers ON
numbers.n <= LENGTH(users.groupids) -
LENGTH(REPLACE(users.groupids, ',','')) + 1
JOIN menus ON
FIND_IN_SET(SUBSTRING_INDEX(
SUBSTRING_INDEX(users.groupids, ',', numbers.n),
',', -1), menus.groupids)
WHERE
users.id = 1
ORDER BY menus.id;
--
felix
Nov 8 '05 #2
Thanks for your detailed answer Felix.

This doesn't look like the direction inwhich I want to go. Looks like
you needed another table anyway. I think I'll create userGroup tables
for Menu and Users and see how I do...

Nov 8 '05 #3
On 8 Nov 2005 05:28:10 -0800, in mailing.database.mysql "rhamlin"
<rh*****@eaglebrass.com> wrote:
| I'm fairly new to creating complex sql statements and need a little
| help.
| In plain english I want to do this: Select menu rows where the row's
| userGroupIDs equal the user's userGroupIDs.
|
| Menu.userGroupID has multiple values seperated by commas.
| User.userGroupID also has multiple values seperated by commas. I
| realize some may want me to create seperate tables for these multiple
| values. The reason I'm using comma seperation at the moment is because
| I used a multi value <select> on a web page to collect and display the
| choices from a single table.
|
| Could I get an example of how to do this using a comma seperated field?
|
| Many thanks.

Have a look at find_in_set function. The only drawback is that there
can be no spaces within this list i.e find_in_set(id,'1,2,3') not
find_in_set(id,'1, 2, 3')
---------------------------------------------------------------
jn******@yourpantsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------
Nov 8 '05 #4

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

Similar topics

5
by: Ralph Freshour | last post by:
I have a question about the following PHP script - I got it off a web site tutorial on how to count users logged into your site - my question is the $PHP_SELF variable - it writes the name of the...
0
by: Stephen Witter | last post by:
I have a multiple select list and I am trying to post to a page where I have no option to use the request.form or vbscript to iterate through the control. The page is a sql server reporting...
7
by: Guy Hocking | last post by:
Hi there, I have a problem in my ASP/SQL Server application i am developing, i hope you guys can help. I have a ASP form with list boxes populated by SQL tables. When a user selects a value...
6
by: Ben Hallert | last post by:
Hi guys, I'm trying to figure out what bone headed mistake I made on something I put together. I've got a form (named 'context') that has a variable number of select-multiple inputs on it. ...
15
by: grunar | last post by:
After some thought on what I need in a Python ORM (multiple primary keys, complex joins, case statements etc.), and after having built these libraries for other un-named languages, I decided to...
1
by: jamminc | last post by:
Hi All, I am currently developing a module where the user will input multiple items (as many as 20-600) and I am suppose to retrieve it from a sql database all the information and update it with...
29
by: pb648174 | last post by:
I have the following basic statements being executed: Create a temp table, #TempPaging Insert Into #TempPaging (Col1, Col2) Select Col1, Col2 From SomeOtherTable Order By Col2, Col1 Select...
4
by: Ian Richardson | last post by:
Hi, The function I've put together below is a rough idea to extend a SELECT list, starting from: <body> <form name="bambam"> <select id="fred"> <option value="1">1</option> <option...
4
by: Matt Ratliff | last post by:
Hello, I would appreciate any assistance you have with the following problem: I have (as an example) an array of values as follows: arrayvalues=new Array("0001","0003","0005") where each is the...
3
by: sangam56 | last post by:
Hello!I am using following sql statement: SELECT Menu.MenuID,Menu.TextUrl FROM Menu WHERE Menu.MenuID= (SELECT Permissions.MenuID FROM Permissions WHERE Permissions.RoleID=(SELECT Roles.RoleID...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.