简体   繁体   中英

Comma separated values into select query

I'm new to PHP and i'm trying to create a small app for my work but i'm stuck few days now because i have a problem and i can't find a solution.

I have 2 tables first jobs_userac that contains 3 fields:

id, user, jobsassigned

and the second jobs_data that contains 5 fields:

id, job_type, description, comments, client_name

Every row in jobs_data contain a task

1 - Offset  - Blablablabla - Blablablabla - Chris
2 - Plotter - Blablablabla - Blablablabla - Nick
3 - Design  - Blablablabla - Blablablabla - John

For every user i have one row in jobs_userac that contains the user id, username, and the assignes jobs in one field comma separated.

1 - Chris - 1,2
2 - Peter - 2,4,5
2 - Maria - 4

Every time a user is connected i store the user id into a variable $user. I want to create a query like this

$result = mysqli_query($con,"SELECT * FROM jobs_data");

But i want to check if the connected user has a row in jobs_userac for example if the connected user is Chris to show him only the jobs from task_data where the job_type is 1 or 2 and if Maria is connected to show her the rows from jobs_data where the job_type=4. I want to add the WHERE clause into my query and if the user has more that one joobs assigned in jobs_userac--> jobsassigned to add the OR clause.

For user Maria

$result = mysqli_query($con,"SELECT * FROM jobs_data WHERE job_type=4");

For user Chris

$result = mysqli_query($con,"SELECT * FROM jobs_data WHERE job_type=1 OR job_type=2");

For user Peter

$result = mysqli_query($con,"SELECT * FROM jobs_data WHERE job_type=2 OR job_type=4 OR job_type=5");

Any help will be very much appreciated.

Your data structure is not ideal - your jobs_userac should have one row per user<->job link.

But you can work with what you have:

SELECT * FROM `jobs_data` WHERE FIND_IN_SET(`job_type`, "2,4,5")

I am not sure for the column of user at jobs_userac whether it is storing a userId or username

here it is assuming that user are storing username or if not then kindly replace the username with the user id.

select * from jobs_data where IN(select jobsassigned 
                                 from jobs_userac 
                                 where user = 'Perter');

I hope this helps you.

Additionally store jobsassigned of each user on connection in $jobsassigned Now you can do the following query:

$queryString = 'select * from jobs_data where id IN ('.$jobsassigned.')';

By the way... it is better to add an extra table where you store youre relations between user and jobs

jobs

1 - Offset  - Blablablabla - Blablablabla - Chris
2 - Plotter - Blablablabla - Blablablabla - Nick
3 - Design  - Blablablabla - Blablablabla - John

users

1 - Chris
2 - Peter
3 - Maria

user_job

1 - 1
1 - 2
2 - 2
2 - 4
2 - 5
3 - 4

As others noted your current schema is sub-optimal and generally speaking not the best way to exploit a relation data base however you can use your current structure in a query with the somewhat clumsy :

"SELECT * FROM JOBS_DATE WHERE id = '2' or id like '2,%' or id like '%,2,%' or id like '%,2'"

You need all thee likes as you have to copy with "2,5,7", "1,2,3" and "1,2" plus the equal for plain "2"

First you have to get the job type assign to the link user:

$result = mysqli_query($con,"SELECT * FROM jobs_useracc where id=".$user);
$row = mysql_fetch_array($result);
$job_types=$row['jobassigned'];
$result = mysqli_query($con,"SELECT * FROM jobs_data where id IN (".$job_types.")");

If each job is only every associated with one user, the id of that user should be stored against the job. so your tables should be id, user and id, job_type, description, comments, client_name, user_id . Then you can get the jobs for Chris by doing SELECT * FROM jobs_data WHERE user_id = 1 . This is a normal pattern for where you have many entries in one table associated with one entry in another.

You are referring to job_type instead of id .

You should follow :

$result = mysqli_query($con,"SELECT * FROM jobs_data WHERE id=2 OR id=4 OR id=5");

And same for others ie use id at place of job_type .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM