SQL SEARCHING relational database - mysql

given a users qualification id's how would you find the jobs they can do using SQL?
1) for example a user with only qualification 1 could only do job3
and not 1 and 4 because you need more than one qualification.
2) a user with qualifications 1 and 2 could do jobs 1 and 3 but not 4
JOBDETAILS TABLE
JobID ,QualificationID
1, 1
1, 2
3, 1
4, 1
4, 2
4, 3
thanks for any help
TJ

SELECT DISTINCT JobID
FROM JobDetails
WHERE QualificationID IN (#Quals)
AND JobID NOT IN
(
SELECT DISTINCT JobID
FROM JobDetails
WHERE QualificationID NOT IN (#Quals)
)
(Apologies for syntax issues; I work with SQL Server, not MySQL)

Related

SQL Query, get all ids that do not have a given status value

I have a table called statuses shown below. Every time a user logs in a row is added. A status is added. I need to write a query that gives me all IDs one time that have never been 1, 6 or 8.
id
status
1
1
1
6
1
8
1
1
2
1
2
6
2
8
2
0
3
0
3
0
3
0
3
0
I wrote a query:
SELECT DISTINCT(id) FROM statuses WHERE status NOT IN (1, 6, 8)
Unfortunately this gives me ID 2 and 3, but I don't want ID 2 to be selected because they have statuses 1, 6, and 8.
Thanks for your help!
You can group by id and set the condition in the HAVING clause:
SELECT id
FROM statuses
GROUP BY id
HAVING SUM(status IN (1, 6, 8)) = 0
Or:
SELECT DISTINCT id
FROM statuses
WHERE id NOT IN (SELECT id FROM statuses WHERE status IN (1, 6, 8))
See the demo.

How to create duplicate records with incremental week numbers

I am trying to create n duplicates(say 5) of a each record from a table with an increment on the week number associated with each record.
Say there is a table with the following as columns - week, id
and there is a record - 1, John
I want the record for John to repeat 5 times to obtain -
1, John
2, John
3, John
4, John
5, John
Well, you can generate a table with five columns and then use that:
select (t.week + x.inc) as week, t.name
from t cross join
(select 0 as inc union all
select 1 as inc union all
select 2 as inc union all
select 3 as inc union all
select 4 as inc
) x;

MySQL query to extract email neighborhood

I have a MySQL table called personxmessage, extracted from emails. It has three fields, id (primary key), MessageID, and PersonID. For each message referenced by MessageID, there are two or more records, each with a PersonID for a person who was included in the email (i.e. in sender, sent to, or cc).
I want to query this table to get a link list showing all the people who were linked to a given PersonID=XXXX, and the links between them defined by being included on the emails that include XXXX, preferably including a weight on the links showing the number of occurrences. Another way of saying this, in graph terminology, is I'm trying to get the neighborhood of XXXX. For example, for entries like
MID PID
1 1
1 2
1 3
2 1
2 2
3 1
3 3
3 4
For PersonID 1 I would like to get link list
P1 P2 Count
1 1 3
1 2 2
1 3 2
1 4 1
2 3 1
3 4 1
Is it possible to do this with some kind of self-join? If so what query would I use? I have done some simpler joins (for example to get the star-graph of XXXX and other PersonIDs that are with XXXX on emails) but this one is over my head.
You can use GROUP_CONCAT do to something sort of like that. It's not a table but a result set of the related pids per mid.
select mid, group_concat(distinct pid order by pid separator ', ') as related_ppl
from personxmessage
group by mid;
result
mid related_ppl
1 1, 2, 3
2 1, 2
3 1, 3, 4
I think this is what you're looking for:
select p.pid as pid1, pp.pid as pid2, count(*) as cnt
from personxmessagep
left join personxmessagepp
on p.mid = pp.mid
and p.pid < pp.pid
group by p.pid, pp.pid;
result
pid pid2 cnt
1 2 2
1 3 2
1 4 1
2 1
2 3 1
3 1
3 4 1
4 1

MYSQL Left Join COUNTS AND SUMS from multiple tables

I have tried looking for similar questions here in SO and have not found any so far. Feel free to tag the duplicate or similar question to this one if you'd like.
I want to join several columns to display a report.
I have 3 tables.
Users
ID Name
1 Chris
2 John
3 Rick
InMessages
ID Content
1 Hello1
2 Hello2
3 Response1
4 Response2
5 Hello3
6 Hello4
OutMessages
ID UserID InMessageID Content CurrentRate
1 1 1 ReplyHello1 10
2 2 2 Reply1Hello2 10
3 3 2 Reply2Hello2 10
4 3 2 Reply3Hello2 10
5 1 3 ReplyResponse1 10
6 2 4 ReplyResponse2 10
7 1 5 ReplyHello3 4
8 3 6 ReplyHello4 4
And the report I'd like to see would be something like:
User InMessagesCount OutMessagesCount Rate10 Rate4
Chris 3 3 2 1
John 2 2 1 1
Rick 2 3 2 1
I have tried the existing queries like:
count(distinct InMessages.ID) as InMessagesCount,
count(distinct OutMessages.ID) as OutMessagesCount
But I am stuck as to how I could make the one for the rates.
Thanks in advance!
You issue seems to be that your multiple joins are creating duplicate records, hence needing DISTINCT in your current counts. But you can't use that for counting the number of occurrences of a non distinct field
As such I would be tempted to try something like this:-
COUNT(DISTINCT IF(OutMessages.CurrentRate = 10, OutMessages.ID, NULL) ) AS Rate10,
COUNT(DISTINCT IF(OutMessages.CurrentRate = 4, OutMessages.ID, NULL) ) AS Rate4
Ie, if the id is 10 then use the id, otherwise use NULL. COUNT like this should (I think) only count the non null values.
For Mysql you can use sum() with expression so it will evaluate as a count that you need for your 2 rates
select
u.Name,
count(distinct o.InMessageID) as InMessagesCount,
count(distinct o.ID) as OutMessagesCount,
sum(o.CurrentRate = 10) rate10,
sum(o.CurrentRate = 4) rate10
from OutMessages o
joins users u on(u.id = o.user_id)
group by u.id
You can sum up those records that match a specific rate:
SUM(IF(OutMessages.CurrentRate = 10, 1,0)) AS Rate10,
SUM(IF(OutMessages.CurrentRate = 4, 1,0)) AS Rate4
You can use IF and SUM, like this:
SELECT UserID,
COUNT(DISTINCT InMessages.ID) AS InMessagesCount,
COUNT(DISTINCT OutMessages.ID) AS OutMessagesCount,
SUM(IF(OutMessages.CurrentRate = 10, 1, 0)) AS Rate10,
SUM(IF(OutMessages.CurrentRate = 4, 1, 0)) AS Rate4
FROM OutMessages
GROUP BY UserID

I want jobs matching with same skill_ids,location of the job_id parameter skill_id (jobid is parameter)

I have one table jobs with fields
job_id skill_ids location
1 1,2,3 1, 3
2 2, 3 2
3 1, 4 4
I want jobs matching with same skill_ids,location of the job_id parameter skill_id (job_id is parameter)
skill_ids are comma separated so it should match at-least 2 skills
First of all your tables should follow Normalization
So the tables should be:
Job:
job_id job_name
1 Job One
2 Job Two
3 Job Three
Skills:
skill_id skill_name
1 Skill One
2 Skill Two
3 Skill Three
Location:
location_id location_name
1 Location One
2 Location Two
Job_skill
job_id skill_id
1 1
1 2
1 3
2 2
2 3
Then you can do Mysql Join
SKILL_ID = 'Your desired SKILL ID';
SELECT * FROM Job JOIN job_skill on job_skill.job_id = job.job_id WHERE job_skill.skill_id = SKILL_ID;
This is only for JOB and SKILL, you can work a little more to make it work with Locations table:
Job_location
job_id location_id
1 1
1 3
2 2
3 4