here still got lots to learn thanks for the help in advance. *bows
tried several time cant seems to get it to work hope to get some help from more experienced coders
I want to show staff that are not teaching anyone
enter image description here
original table
Staff_ID Mentor_ID First_Name Last_Name
----------- ----------- ------------------------- -------------------------
101 NULL May Ou
102 101 Io Rush
103 102 Ley Have
104 103 Stephanie Soon
105 101 Iat Raiz
106 101 Rina shion
--notes
not having a mentor id they are not learning from anyone
having a mentor id learning that that person e,g 102 staff "Io Rush"
is learning from staff 101 "May Ou" therefore means staff 101 is
teaching 102 as well.
therefore if staff id of a staff did not appear in mentor id they are
not teaching anyone e.g. staff 104, 105 & 106
--basic key points
staff 101 is not learning from any one but is teaching
other people
staff 102,103,104,105,106 are all learning from someone
staff 101,102,103 are the only staff that are teaching other staff
staff 104,105,106 are the only ones not teaching anyone
--my codings:
SELECT *
FROM staff
WHERE Staff_ID IN (SELECT Mentor_ID FROM staff)
--Result
enter image description here
Staff_ID Mentor_ID First_Name Last_Name
----------- ----------- ------------------------- -------------------------
101 NULL May Ou
102 101 Io Rush
103 102 Ley Have
-- this show the complete opposite of what i am going for as this show all staff that is teaching someone
-- so i tought of an idea an since its the opposite should be just adding a NOT will give me what i one so i type the following code
--coding 2 :
SELECT *
FROM staff
WHERE Staff_ID NOT IN (SELECT Mentor_ID FROM staff)
--result:
blank....
no idea what else i can do --" kinda stuck now, btw i am still a newbie to mysql so there may be i function of stuff that i dont know of please bear with me*bows
yup so that is a detailed description of my problem, i thank all those in advance for taking your time to read my problem all advice is appreciated, hope to hear from you guys soon *waves waves :D
Try this one:
SELECT * FROM staff s WHERE NOT EXISTS (SELECT 1 FROM staff m WHERE m.Mentor_ID=s.Staff_ID )
Your last query was in a way similar, but you fell into a common pitfall with the NOT IN construct. It works fine as long as there are no null values in your list you are comparing against. But unfortunately there is a null value. The above form with EXISTS will always work.
I changed your data for a little fiddle (using 0 instead of null) and you can see that both versions work there.
EXISTS is much faster than IN when the subquery results is very large.
IN is faster than EXISTS when the subquery results is very small.
This will help you Difference between EXISTS and IN in SQL?
Related
I have 2 tables as follows
Human
id name
1 John
2 mark
Computer
id human_id
1 2
2 null
I want my answer to be as following
name no_of_computer
John 0
mark 1
I tried the following but am wrong
select h.name,c.human_id) as 'no_of_computer' from human h
join
computer c on c.human_id = h.id
group by h.id
my code is not counting the null as 0 so its just avoiding that and only showing that mark has 1 computer, but I want to see John has 0 computer too. please help me out, thanks in advance
You can use a subquery that's something like:
SELECT
h.Name,
(SELECT COUNT(c.id_of_computers)
FROM computers c
WHERE c.id_human = h.id) as no_of_computer
FROM humans h
Do not cut and paste this to test, as I paid no attention to actual ID names in your tables. You'll have to look at that. 👍
id_no doc_id item_no product customer
123 2 1 A Daisy
123 2 9 A Ben
123 4 3 A Daisy
123 4 4 A Ben
123 6 11 B Daisy
123 6 13 B Ben
when I put it in my report it results to
Daisy Daisy
Ben
And it is also the result in mysql
select distinct customer from receipt where id_no like '123'
result:
Daisy
Daisy
Ben
Another query that I tried:
select distinct id_no, customer, product from receipt where id_no like '123'
result:
123 Daisy A
123 Daisy B
123 Daisy A
123 Ben A
123 Ben B
desired result:
Daisy
Ben
Please help me please.
Thank you guys for the help I found out why the other one keeps on showing. It is because the other Daisy is spelled as Daissy that's why.
Most likely your Customer name contains additional characters between the two records. Depending on how the datatype is implemented, spaces could matter and have contributed to the difference.
Try concatenating a character before and after customer.
I am unfamiliar with the concepts in Crystal Reports, but from what I understand, you would have to create a formula like so:
"XXX" & {Receipt.Customer} & "XXX"
If you run it again, you might recognize there is additional space like so:
XXXDaisyXXX
XXXDaisy XXX
^____ Additional Space
There is no chance of error while you using distinct ..it should return distinct value ...any way you can try another way
SELECT customer FROM receipt WHERE id_no like '123' GROUP BY customer
I don't see why you are fetching three records. I tried implementing your database and ran your query. It returned the result as expected.
See the above pic. There may be some issue with the data type you used. You may try grouping via customer, but I don't think it should affect your result anyway.
Also Check if the data types match.
The selection you made from customer id and id_no is unique and with distinct it should return only two rows
plase try this code
i get solution
select distinct `customer` from receipt where `id_no`='123'
this is right
i tryied this is my past project
best of luck
i apologize for the bad topic title as i am kinda at lost of what should i do here.
First of all, here are my database table designs and i would like to receive some sort of feedback as well before proceeding:
I am trying to make an enrollment web application.
Subjects
subject
[id] [subj] [professor] [cstart] [cend] [days] [count] [units]
1 comalgo carl 10:00am 12:30pm M-W 40 3.0
2 compasm carl 01:00pm 02:30pm T-TH 40 3.0
3 compro miguel 04:30pm 06:30pm M-W 35 3.0
4 pro5 fua 03:30pm 05:30pm T-TH 30 3.0
5 pro5 fua 06:30pm 08:30pm F 10 3.0
Students
students
[id] [fname] [lname] [bday] [unitsleft] [unitstaken] [major]
1 carlos doe 11/20/1990 100 0 BS-COMPUTER SCIENCE
2 miguel doe 08/15/1992 100 0 BS-ECONOMY
3 carl doe 12/12/1991 100 0 BS-PSYCHOLOGY
4 test doe 02/12/1992 100 0 BS-LITERATURE
What i plan on doing is that, the subjects will be presented through a series of list and the student will pick multiple subjects and enroll. The total enrolled units should not be greater than 20 and less than 11. I already implemented that part and i am now working on storing the selected subjects.
Here is my proposed solution and i would like to receive some feedbacks about it and what operation should i use.
I will have a centralized table for all of enrolled subjects
Enrolled subjects
selected subject
[id] [subjid] [studentid] [status]
1 1 1 P //comalgo enrolled by carlos, P is for pending grade
2 1 2 P //comalgo enrolled by miguel P is for pending grade
3 2 1 P //compasm enrolled by carlos P is for pending grade
What i plan to happen is that: i have a profile page for the students and they will be able to view/edit their currently enrolled subjects specifically for themselves(based from ID)
I want them to be presented with the following table headers which is based from the enrolled subjects:
[subj] [professor] [cstart] [cend] [days]
Here are the brief summary of my questions:
1.) is my table alright? or it's a bad design?
2.) What kind of method should i use? i am trying to research about it(joins) but i am somehow confused and in need of clarification. I want to present the users with their selected subjects.
edit: i think i can do something like.. select where id = x from the enrolled subjects then get the subject id but i am not sure if that will be efficient.
Your tables look good (i.e. third normal form). An INNER join will do
SELECT
B.subj, B.professor, B.cstart, B.cend, B.days
FROM
selectedsubjects A
INNER JOIN subjects B ON A.subjid = B.id
WHERE
A.studentid = 1
p.s. This looks like an assignment for school or something out of a book...?
It could be:
var query = context.EnrolledSubjects
.Include("Subject")
.Where(i => i.StudentId == 1)
.Select(i => new {
i.Subject.Subj,
i.Subject.Professor,
i.Subject.CsStart,
i.Subject.Send,
i.Subject.Days,
i.Status
};
I have a simple question that I wasn't really sure how to search for (or title!). I apologize if this has been asked a million times. For the following table, how do I generate a report that will detail the number of companies that a person has worked for and how many people have also worked for that same number? So, for example, this table should return:
people, companiesperperson
1, 1
2, 2
1, 3
for the following table called personalinfo:
id_number first last company
1 John Doe Intel
2 John Doe Microsoft
3 Phil Jenkins Amgen
4 Phil Jenkins Bayer
5 Phil Jenkins Sanofi
6 Josh Edwards Walgreens
7 Amy Dill URS
8 Amy Dill ARCADIS
Let me know if this is still confusing and if I can further clarify what I am looking to do.
Thanks!
This is a rough estimate of the query but
SELECT count as companiesperperson, COUNT(first, last) as people FROM
(SELECT COUNT(company) as count, first, last FROM personalinfo GROUP BY (first, last)) as a
GROUP BY count
To explain the query first in the subquery we are asking for the names and count of companies after splitting up all the rows by names
Then in the outer query we split up all the rows by their count and ask how many unique names can be found in each group.
There may be a few syntax errors I've left straggling but the group by feature is really what's essential to understanding how to solve this question.
id name age
2 Sandy Smith 21
4 Alie Smith 19
5 James Smith 11
Hello, I am new to programming and am watching tutorials online this summer in my free time. I know this is a simple question but I've searched online and I can't find exactly what I need.
I deleted a row so now i no longer have "id 2 3 4 5". I'm left with 2, 4 ,5". I don't know the MySQL query to place alie smith as #3 and james smith as #4. Please help me out with this query. Thanks
If you still have the original insert statements or can re-create them, you can do a TRUNCATE TABLE [tablenamehere] then reinsert the data. Could be handy if you're trying to get the table back to a known state.
you don't need to. The point of a primary key is that is UNIQUE. ID field will auto increment on its own. The only way to do this would be to take the primary key attribute off of the ID column so that you can control it manually, insert the correct id's, and then put the key on. Read up on normalizing databases and it will make more sense.