Table Name: EFL_EMP
Company Locality Geo Name Code address
EFL SINDH 10023 Ali 100 main House HYD
EFL SINDH 10024 Adil 101 main House HYD
EFL SINDH 10025 Zahir 102 main House HYD
EFL SINDH 10026 Naeem 103 main House HYD
EFL SINDH 10027 Khalid 104 main House HYD
I Want to Update Geo Where Code Is 100, 102 OR 104 in one query.
Please guide me how.
This is very simple SQL, that you could easily work out for yourself with some basic research. You haven't really provided sufficient information in your question for a solution that will definitely work, but the syntax you are after is as follows:
update EFL_EMP
set Geo = 'Value to update to'
where Code in(100,102,104)
Related
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?
Hopefully this will make sense...I have a table in Access 2010 that contains a list of suppliers and their point of contacts at the supplier and where I work. The POCs vary in number, anywhere from 1-4 up to this point. The table is set up so each POC is on a separate line.
The supplier could have one contact but work could have three different contacts and vice versa.
What I want to happen is when I select a value from a combobox on a form, all the related POCs need to be shown instead of cycling through them one by one.
For example, Supplier1 has two POCs at their facility and we have three at our facility. I would like to have the combobox find Supplier1 in the table and then show all the contacts for that supplier (their facility and ours) in a textbox.
The user will be able to edit the contact information and, if it is not too difficult, would be able to add/delete a contact.
I'm sure a question similar to this one has been asked before, however I have been unable to word it correctly to find a solution through google searches/this website. I'm comfortable enough with VBA to use that if required but am by no means an expert. I am completely unfamiliar with SQL and would like to avoid going that direction if at all possible.
I have to be careful with any data I provide but will do what I can if you need to see the data or anything like that.
Supplier Code Part Supplier Contact Procurement Contact QC Contact
Ajin AKVN Patrick Yong Jack
Ajin AKVN Chase Yong Jack
Autoliv AMNP Seatbelt Daryl James Lewis
Bosch AG48 Hancheul Kevin
Carlex AKJ5 QTR Glasses Bob Joy Zack
Continental ANKC Jacob
KSR C03A05 Brake Pedal Jose Paul David
KSR C03A05 Brake Pedal Jose Paul Gary
KSR C03A05 Brake Pedal Jose Paul Steven
KSR AG5Z Accelerator Pedal Jack Paul David
KSR AG5Z Accelerator Pedal Jack Paul Gary
KSR AG5Z Accelerator Pedal Jack Paul Steven
KSR AG5Z Accelerator Pedal Cory Paul David
KSR AG5Z Accelerator Pedal Cory Paul Gary
KSR AG5Z Accelerator Pedal Cory Paul Steven
Your table needs heavy normalization (see e.g. What is Normalisation (or Normalization)? or http://r937.com/relational.html )
I would suggest (Note: I'm not sure about the Supplier/Code/Part relation) :
- tSupplier
SupplierID SupplierName
1 Ajin
2 KSR
- tParts
PartID SupplierID Code Part
1 1 AKVN
2 2 C03A05 Brake Pedal
- tContactTypes
TypeID Type
1 Supplier
2 Procurement
3 QC
- tContacts
ContactID SupplierID TypeID ContactName
1 1 1 Patrick
2 1 1 Chase
3 1 2 Yong
4 1 3 Jack
and so on.
The first column of each table is the primary key, an autonumber field.
All other ID columns are foreign keys, linking to a parent table.
Now you can have a combobox for the Supplier, which gives the SupplierID.
With that, you can filter the Contacts and show them in a datasheet subform.
Either all in one table, with the ContactTypes as column, or in three subforms, each filtered by one ContactType.
To be able to add new contacts, use the BeforeInsert event to assign the current SupplierID.
I have a list of data that happens to have been set up like this:
entryID fieldID Value
100 1 John
100 2 Smith
100 3 USA
101 1 Jane
101 2 Doe
101 3 USA
The way it works is, when a "ticket" is created, it assigns an entryID for all the data in that ticket, but each field also has it's own fieldID. So first name is always a fieldID of 1, last name is 2, etc.
What I need to be able to do is create a view that will look like this:
First Name Last Name Country
John Smith USA
Jane Doe USA
I need to be able to do this in MySQL, and not SQL or excel, as some other similar problems have addressed.
I just found the answer to my own question, so I will post it here for future users.
This website explains how to do exactly what I was asking for: http://stratosprovatopoulos.com/web-development/mysql/pivot-a-table-in-mysql/
I tried to create a VIEW that merge some tables in order to have the "hottest" teacher in a educational platform.
First, I have a table with the users (some teachers, some students),
then in other I have the lessons created by the teachers,
finally, other one that has the relation between students and lesson.
when I use this SQL sentence
CREATE OR REPLACE VIEW `skn_teachers` AS
select
`u`.`id_skn_users` AS `id_skn_users`,
`u`.`firstName` AS `firstName`,
`u`.`lastName` AS `lastName`,
COUNT(`ls`.`createdBy`) AS `countLessons`
from (`skn_users` AS `u`, `skn_rolesxusers` AS `rxu`, `skn_roles` AS `ro`, `skn_approved_lessons` AS `ls`)
where ((`rxu`.`id_skn_users` = `u`.`id_skn_users`) and (`rxu`.`id_skn_roles` = `ro`.`id_skn_roles`) and (`ro`.`name` = 'teacher') and (`ls`.`createdBy` = `rxu`.`id_skn_users`))
group by `u`.`id_skn_users`
the row countLessons show me the number of lessons per teacher eg.
id | firstName | lastName | countLessons
1 Pepito Perez 1
2 Julián Figueroa 7
3 Daniel Aguirre 2
which is correctly the number of lessons per teacher.
but I need the number of students that have the lessons created by each teacher (all of them, sum of all students in all lessons of THAT teacher), countStudentsByTeacher, in one of my attempts, get this SQl and it was a surprise when I got the number of students by teacher but I don't understand clearly what I did.
new SQL sentence:
CREATE OR REPLACE VIEW `skn_teachers` AS
select
`u`.`id_skn_users` AS `id_skn_users`,
`u`.`firstName` AS `firstName`,
`u`.`lastName` AS `lastName`,
COUNT(`ls`.`createdBy`) AS `countStudents`
from (`skn_users` AS `u`, `skn_rolesxusers` AS `rxu`, `skn_roles` AS `ro`, `skn_approved_lessons` AS `ls`, `skn_lessonsxusers` AS `lxu`)
where ((`rxu`.`id_skn_users` = `u`.`id_skn_users`) and (`rxu`.`id_skn_roles` = `ro`.`id_skn_roles`) and (`ro`.`name` = 'teacher') and (`ls`.`createdBy` = `rxu`.`id_skn_users`) and (`ls`.`id_skn_lessons` = `lxu`.`id_skn_lessons`))
group by `u`.`id_skn_users`
//
id | firstName | lastName | countLessons
1 Pepito Perez 10
2 Julián Figueroa 15
3 Daniel Aguirre 8
here, column countLessons shows really the number of students into all lessons created by each teacher, exactly what I wanted but I want to know why this works.
Thanks in advance!
In your 2nd query, you added at join to the table skn_lessonsxusers. This causes the 2nd query to return additional rows per each student that was assigned the lesson. The first query only returned the lessons that were created by each teacher.
Example:
Looking at the teacher "Daniel Aguirre", the underlying non-aggregated data for the first query might look like this:
id | firstName | lastName | Lesson
3 Daniel Aguirre Math 101
3 Daniel Aguirre CS 104
So there are 2 lessons that he teaches.
Now if you add in the students for each lesson, i.e. the 2nd query, then the data might look like this.
id | firstName | lastName | Lesson | Student
3 Daniel Aguirre Math 101 John
3 Daniel Aguirre Math 101 Bob
3 Daniel Aguirre Math 101 Sara
3 Daniel Aguirre Math 101 Mary
3 Daniel Aguirre CS 104 John
3 Daniel Aguirre CS 104 Bob
3 Daniel Aguirre CS 104 Dan
3 Daniel Aguirre CS 104 Sally
So now when you aggregate this 2nd set of data, it will show a count of 8 because there are 8 student/lesson combination for Daniel Aguirre.
Now assuming that John and Bob in the above 2nd data set are the same student, there are not really 8 different students taking lessons from Daniel Aguirre, there are 6 (John, Bob, Sara, Mary, Dan, and Sally). If you wanted to show 6 in this example you would use:
COUNT(DISTINCT `lxu`.`student_id`) AS `countStudents`
where student_id is the column that represents a unique student in the skn_lessonsxusers table. NOTE: your column is probably a different name than "student_id", but I don't know your table structure for this example so I just used "student_id".
It works because you multiply each lesson by each student in this lesson
join... 'skn_approved_lessons' AS 'ls'
where... and ('ls'.'createdBy' = 'rxu'.'id_skn_users')
I have two tables and I need to list the number of complaints by each rental number.
the tables I have are the following:
RENTAL TABLE
RENTAL_NO RENTAL_DA R LEA LEASE_STA LEASE_END STAFF APT_NO
100101 12-MAY-07 O One 01-JUN-07 31-MAY-08 SA200 201
100102 21-MAY-07 O Six 01-JUN-07 30-NOV-07 SA220 102
100103 12-OCT-07 O Six 01-NOV-07 30-APR-08 SA240 203
100104 06-MAR-08 O One 01-APR-08 31-MAR-09 SA210 101
100105 15-APR-08 O One 01-MAY-08 30-APR-09 SA220 104
100106 15-JUL-08 S One 01-AUG-08 31-JUL-09 SA200 100
Complaints table
COMPLAINT_NO COMPLAINT RENTAL_COMPLAINT APT_COMPLAINT RENTAL_NO APT_NO S
10010 12-DEC-07 kitchen sink clogged 100103 203 F
10011 17-AUG-08 water heater not working 100105 104 F
10012 17-SEP-08 room heater problem 100105 104
10013 17-SEP-08 AC not working 103
10014 20-OCT-08 car parking not proper 100103
10015 08-NOV-08 delay in payment 100104 F
10016 16-NOV-08 utility not working 202
===================================================================================
so I will need something like this:
apt_no Number of complaints
10000 3
10100 1
.
.
.
Im still a newbie and I've been trying a lot and I dont seem to get it right. any help will be appreciated.
try this out:
select APT_COMPLAINT, count(complaint_no) as CComplaint
from complaints
group by APT_COMPLAINT
With info you provided, I can only guess what are relations between your tables. But if Compliants.APT_NO is FK for Rental.RENTAL_NO, you gonna need this code
SELECT r.RENTAL_NO apt_no, count(*) Number_of_complaints
FROM Rental r JOIN Complaints c
ON r.APT_NO = c.RENTAL_NO
GROUP BY r.APT_no