I have this one table which is table staff. The column is id_staff, no_staff and id_posting. I would like to count how many staff using the same id_posting. Can anyone help me with the query?thanks.
Do you have tried to make the query yourself?
This is quite basic
select count(id_staff), id_posting from staff where id_posting = ? group by id_posting
Please try this
SELECT id_posting ,COUNT( `id_staff` ) as cnt_staff
FROM table_staff
GROUP BY id_posting
select COUNT(*) AS ID_COUNT from YOUR_TABLE group by THE_ID_WHICH_IS_REPEATED
http://www.w3schools.com/sql/sql_func_count.asp
check the tutorial for more info.
Related
I am having some strange issue with the SQL statement below. The result groups by user IDs and some of them turn out right but for one of them (user ID = 1) the "initial_average" is multiplied by 3. I really have no idea why.. Is there something wrong with the structure of the statement? If it is not clear, the aim is to sum the field "initial_avg" in the "tasks" table and have it broken out by user. Some help with this is much appreciated. I am using MySQL.
SELECT sum(initial_avg) AS initial_average
, sum(initial_std) AS initial_standard_dev
, tasks.user
, hourly_rate
FROM tasks
INNER JOIN user_project
ON tasks.user=user_project.user
AND tasks.project=59
AND tasks.user=1
GROUP BY tasks.user
I just solved it by adding another "and" clause (AND user_project.project=59 )
Optimize your query (Try it):
SELECT SUM(initial_avg) AS initial_average, SUM(initial_std) AS initial_standard_dev, tasks.user, hourly_rate FROM tasks INNER JOIN user_project ON tasks.user = user_project.user AND tasks.project = User_project.project WHERE tasks.project = 59 AND tasks.user = 1 GROUP BY tasks.user, hourly_rate
As search filtering is not working when use findbysql in yii2 searchmodel, so I want to write an equivalent query of "SELECT * FROM challan WHERE id IN (SELECT MAX(id) FROM challan GROUP BY sid)" in Yii2
$query->andWhere(new Expression('id IN (SELECT MAX(id) FROM challan GROUP BY sid)'));
I have found answer after spending many hours and here it is
Challan::find()->Where(['challan.id' => Challan::find()->select(['MAX(id)'])->groupBy('sid')]);
I have the following SQL query for MySQL:
SELECT SQL_CALC_FOUND_ROWS objects.objects_no
FROM objects
LEFT JOIN finds ON (objects.objects_no = finds.objects_no)
LEFT JOIN ceramics ON (objects.objects_no = ceramics.objects_no)
WHERE 1=1
and (objects.objects_no) in (select DISTINCT objects_no from objects_materials where thesaurus_term_id in (18658))
and (objects.objects_no) in (select DISTINCT objects_no from objects_objects where thesaurus_term_id in (24193))
GROUP BY objects.objects_no
ORDER BY objects.objects_no
Instead of getting results that match both subqueries, I also get results that match one or the other. Does anyone have an idea why that is?
Thanks, Sandro
Try parenthesizing the conditions.
WHERE (
(1=1)
and ((objects.objects_no) in (select DISTINCT objects_no from objects_materials where thesaurus_term_id in (18658)))
and ((objects.objects_no) in (select DISTINCT objects_no from objects_objects where thesaurus_term_id in (24193)))
)
Thanks for all your help. It actually does work just fine. There was a problem within the data inside the thesaurus.
Sorry!!!
I am new to databases and i really stuck! Please give me a hand! Have no idea, where I made a mistake...
I have 2 tables patient and caretaker
they both have lastname and firstname
I need to retrieve lastname and firstname from both of them and i made the following query:
SELECT firstname
FROM mortenu8.patient, caretaker
where caretaker.firstname = patient.firstname;
But it says
Error Code: 1052. Column 'firstname' in field list is ambiguous 0.034 sec
Do you have any idea why? I will really appreciate your help...
Thanks!
Use Database Objects.
When you specify the firstname in the columns list where both the tables have the same column name, data base engine cannot recognize the first name of which table exactly are you trying to retrieve!!
Many of the above answers says the same thing.
Just to reiterate the same,
SELECT patient.firstname, ctaker.firstname
FROM mortenu8.patient patient, caretaker ctaker
WHERE ctaker.firstname = patient.firstname;
try this:
SELECT patient.firstname
FROM mortenu8.patient, caretaker
where caretaker.firstname = patient.firstname;
Assuming your query of join is working you can try this
SELECT patient.firstname,caretaker.firstname
FROM mortenu8.patient, caretaker
where caretaker.firstname = patient.firstname
OR
SELECT caretaker.firstname
FROM mortenu8.patient, caretaker
where caretaker.firstname = patient.firstname
Try this
SELECT MP.firstname as PatientName,
C.firstname as CaretakerName
FROM mortenu8.patient MP, caretaker C
where C.firstname = MP.firstname;
I want to select all the photoes avoiding gallery name repetations.But using group by,I retrieve all the gallery names but I get only one photo related to every gallery.becoz group by only display only one row.
How can I display all the photoes with out repitation of gallery name?ANy alternative approach?.Plz help.
$sql=mysqli_query($db3->connection,"SELECT * from photo group by gallery_name ");
while($row=mysqli_fetch_array($sql)){
$pic_name=$row['p_name'];
$gallery_name=$row['gallery_name'];
echo "$gallery_name>$pic_name,";
}
ANd second alternative approach can be like here but I am still getting gallery name repitation
$sql=mysqli_query($db3->connection,"SELECT gallery_name from photo group by gallery_name ");
while($row1=mysqli_fetch_array($sql1)){
$gallery_name=$row1['gallery_name'];
$sql=mysqli_query($db3->connection,"SELECT * from photo where gallery_name='$gallery_name' ");
while($row=mysqli_fetch_array($sql)){
$pic_name=$row['p_name'];
$gallery_name1=$row['gallery_name'];
echo "$gallery_name>$pic_name,";
}}
Use 'ORDER BY' instead of "GROUP BY".
SELECT *
...
GROUP BY ...
is wrong. Groups are used for counting, finding minimum and maximums, and other group action.
The rest should depend on your PHP code.
If you are looking for all pictures for a specific gallery, use a WHERE clause without group by like this:
SELECT *
from photo
where gallery_name = '$gallery_name';
Try this::
SELECT gallery_name, p_name from photo group by gallery_name, p_name
I think you might be looking to use the DISTINCT and ORDER BY keywords:
SELECT DISTINCT p_name, gallery_name
FROM Photo
ORDER BY gallery_name, p_name
Good luck.