mysql count number of votes in votes table - php

I have 3 tables named election_cand, candidate and votes:
election_cand table:
ele_can_id | election_id | candidate_id
candidate table:
id | canname | canadd | canphone | canmail | candes | canphoto
votes table:
voteid | candidateid | voterid | electionid
my tables with data are:
I want the desired result as
I write the mysql query as:
SELECT a.*,b.*,c.*, count(voteid) AS numrows
FROM election_cand a
left join votes b on a.election_id=b.electionid
left join candidate c on a.candidate_id=c.id
where a.election_id='$get_ele_id' group by a.candidate_id

As I see you only need this query:
SELECT
c.*, COUNT(e.ele_can_id) AS numrows
FROM candidate c
Left Join election_cand e on c.id = e.candidate_id
GROUP BY c.id
describe your schema so we can help you more.

Try this code:
SELECT
c.*, COUNT(v.candidateid) FROM candidate c
LEFT JOIN votes v ON c.id = v.candidateid
WHERE c.id IN
( SELECT candidate_id FROM election_cand WHERE election_id = 4 )
GROUP BY v.candidateid

Related

Display the latest records from the right table and if records not available then at least show the records from left table

I have two table
tbl_register
tbl_societymyprofile
What I am doing is, I have to display the records from the tbl_register table and I have to display the latest record from the tbl_societymyprofile table.
I have a tried the below query.
SELECT *
FROM tbl_register AS r
LEFT JOIN tbl_societymyprofile AS m
ON r.reg_id = m.reg_id
WHERE r.reg_id =(SELECT Max(secretary_id) AS c_id
FROM tbl_societymyprofile
WHERE reg_id = 17
GROUP BY reg_id DESC)
AND r.is_active = 1
Also if the records are not available in the tbl_societymyprofile then at least display the records from the tbl_register table.
reg_id id is the primary id of the tbl_register table. As of now I have the records in the tbl_register table but don't have the records in the tbl_societymyprofile and i am getting the empty records
tbl_register
reg_id | name | mobileno | email
1 | abc |1234123455 | abc#gmail.com
2 | xyz |1234345455 | xyz#gmail.com
tbl_societymyprofile
secretary_id | reg_id | sec_name | sec_email | date_of_added
1 | 1 | ncx | ncx#gmail.com | 2021-06-09 15:54:35
2 | 1 | xbs | ncx#gmail.com | 2021-06-09 15:55:18
would you help me out with this?
If you need all records, try the following query:
SELECT r.*,
X.*
FROM tbl_register AS r
LEFT JOIN (SELECT sp.*
FROM tbl_societymyprofile sp
INNER JOIN (SELECT reg_id,
MAX(secretary_id) secretary_id
FROM tbl_societymyprofile
GROUP BY reg_id )T
ON sp.reg_id = T.reg_id
AND sp.secretary_id = T.secretary_id) X
ON r.reg_id = X.reg_id
If you need specific records, try the following query instead:
SELECT r.*,
X.*
FROM tbl_register AS r
LEFT JOIN (SELECT sp.*
FROM tbl_societymyprofile sp
INNER JOIN (SELECT reg_id,
MAX(secretary_id) secretary_id
FROM tbl_societymyprofile
GROUP BY reg_id )T
ON sp.reg_id = T.reg_id
AND sp.secretary_id = T.secretary_id) X
ON r.reg_id = X.reg_id
WHERE r.reg_id = 2
If your joined table order is not important (it usually isn't important) then maybe you should try something like:
SELECT * FROM tbl_register t LEFT JOIN tbl_societymyprofile s USING(reg_id) WHERE t.reg_id=2 ORDER BY s.date_of_adder DESC LIMIT 1;
Here's a fiddle. See Query #3.

Mysql multiple join query issue

I have three tables to be join please see this three tables
composite_inventories
composite_has_inventories
inventories
above three table i have join using below query
SELECT u.id, u.purchase_item_name,u.sales_item_name, us.type ,Group_concat(s.itemcode) as Items FROM composite_inventories as u LEFT JOIN composite_has_inventories as us ON u.id = us.composite_inventory_id LEFT JOIN inventories as s ON US.inventory_id = s.id GROUP BY us.composite_inventory_id,us.type
I got output of my above query as below
but instead of above output i need output should be like this
id | Purchase_item_name | sales_item_name | purchase_items | sales_items
-------------+-------+-----------+----------------------------
12 | golden | NULL | A123,Z523,QQ5252 | NULL
13 | test | demoabc | Z523,QQ5252 | Z523
please help me to convert this query to laravel query.
To achieve what you want it's necessary to join to the inventories table twice, once for purchases and once for sales. Then you can do a GROUP_CONCAT on the items from each of those JOINed tables to get purchase_items and sales_items.
This query should give you the desired result (SQLFiddle)
SELECT u.id, u.purchase_item_name,u.sales_item_name, us.type,
GROUP_CONCAT(p.itemcode) AS purchase_items,
GROUP_CONCAT(s.itemcode) as sales_items
FROM composite_inventories as u
LEFT JOIN composite_has_inventories as us ON u.id = us.composite_inventory_id
LEFT JOIN inventories as s ON US.inventory_id = s.id AND us.type='sale'
LEFT JOIN inventories as p ON US.inventory_id = p.id AND us.type='purchase'
GROUP BY u.id
Output:
id purchase_item_name sales_item_name type purchase_items sales_items
12 golden (null) purchase Z523,QQ5252,A123 (null)
13 test demoabc purchase Z523,QQ5252 Z523

MYSQL joining three tables

I have a simple multiple school management system and I am trying to get total number of teachers, and total number of students for a specific school. My table structures are as follows:
teachers
--------------------------
id | schoolid | Name | etc...
--------------------------
1 | 1 | Bob |
2 | 1 | Sarah|
3 | 2 | John |
students
--------------------------
id | schoolid | Name | etc...
--------------------------
1 | 1 | Jack |
2 | 1 | David|
3 | 2 | Adam |
schools
--------------------------
id | Name | etc...
---------------------------
1 | River Park High |
2 | Stirling High |
I can count just all teachers with the following query:
SELECT COUNT(a.id) AS `totalteachers`
FROM teachers a
LEFT JOIN schools b ON a.schoolid = b.id WHERE b.id = '1'
and similarly I can count the number of teachers with the following query:
SELECT COUNT(a.id) AS `totalstudents`
FROM students a
LEFT JOIN schools b ON a.schoolid = b.id WHERE b.id = '1'
I am however struggling with trying to combine these two queries to get a simple result like this:
totalstudents | totalteachers
--------------------------------
2 | 2
I have tried the following:
SELECT COUNT(a.id) as `totalteachers`, COUNT(c.id) as `totalstudents`
FROM teachers a
LEFT JOIN schools b ON a.schoolid = b.id
LEFT JOIN students c ON c.schoolid=b.id WHERE b.id = '5'
You can do something like this
SELECT
id, name, s.total AS totalstudents, t.total AS totalteachers
FROM schools
JOIN (SELECT schoolid, COUNT(id) AS total FROM teachers GROUP BY schoolid)
AS t ON t.schoolid = id
JOIN (SELECT schoolid, COUNT(id) AS total FROM students GROUP BY schoolid)
AS s ON s.schoolid = id
then you can add where id = 2 or whatever to limit the school.
The problem with the multiple left joins is it generates additional records for each teacher to each student; artifically inflating your counts
There's four ways to solve this: (best imo is what Andrew bone did)
Simply select inline without the joins so the counts are not inflated. (most desirable in my mind as it's easy to maintain)
SELECT (SELECT COUNT(a.id) AS `totalteachers`
FROM teachers a
WHERE A.SchoolID = '1') as TotalTeachers
, (SELECT COUNT(a.id) AS `totalstudents`
FROM students a
WHERE a.SchoolID = '1') as TotalStudents
Use subqueries to get the counts first before the joins, then join. Since count will always be 1 a cross join works.
SELECT totalTeachers, totalStudents
FROM (SELECT COUNT(a.id) AS `totalteachers`
FROM teachers a
LEFT JOIN schools b
ON a.schoolid = b.id
WHERE b.id = '1')
CROSS JOIN (SELECT COUNT(a.id) AS `totalstudents`
FROM students a
LEFT JOIN schools b ON a.schoolid = b.id
WHERE b.id = '1')
Use key word distinct within the count so as not to replicate the counts and negate the artificial inflation (least desirable in my mind as this hides the artifical count increase)
SELECT COUNT(distinct a.id) as `totalteachers`, COUNT(distinct c.id) as `totalstudents`
FROM teachers a
LEFT JOIN schools b ON a.schoolid = b.id
LEFT JOIN students c ON c.schoolid=b.id WHERE b.id = '5'
Another way would be to use a window functions, however these are not available in mySQL.
SELECT COUNT(t.id) AS TotalTeachers, COUNT(st.id) AS TotalStudents
FROM schools s
INNER JOIN teachers t
ON s.id = t.schoolid
INNER JOIN students st
ON s.id = st.schoolid
Try this SQL. I havn't try it but it should work.

MySQL: Join three tables and concatenate grouped results

I have following three tables 'doctors' ,'specialities' and 'doctor_specialities':
doctors
-id
-doctor_name
specialities
-id
-speciality_name
doctor_specialities
-id
-doctor_id
-speciality_id
I'd like to return all doctors along with their speciality name and speciality id. A doctor can have multiple specialities.
The result-set should look something like:
id | doctor_name | speciality_id | speciality_name
--------------------------------------------------------------------------------------
1 | John | 5,3 | Speciality1,Speciality2
3 | Tim | 3 | Speciality2
6 | David | NULL | NULL
I tried below query:
SELECT d.id ,d.doctor_name, s.speciality_name, s.id
AS speciality_id
FROM api_doctors d
LEFT JOIN api_doctor_specialities ds
ON ds.doctor_id = d.id
LEFT JOIN api_specialities s
ON s.id = ds.speciality_id
GROUP BY d.id
but in this case I am getting single speciality.
What you're looking for is a MySQL function called GROUP_CONCAT, which returns either a concatenated result or NULL. The default separator is a comma, so the results from this query should match your desired result-set.
SELECT d.id, d.doctor_name, GROUP_CONCAT(s.speciality_name) AS speciality_name, GROUP_CONCAT(s.id) AS speciality_id
FROM api_doctors d
LEFT JOIN api_doctor_specialities ds
ON ds.doctor_id = d.id
LEFT JOIN api_specialities s
ON s.id = ds.speciality_id
GROUP BY d.id
Schema and query on SQL Fiddle

MYSQL QUERY to join multiple tables

I am looking for single query where I can connect multiple tables.
The Query is as follows
`
SELECT
a.name as module_name,
a.id,
b.id as subject_id,
b.SUBJECT_name,
c.id as course_id,
c.course_name,
d.id as cordinator_id,
d.cordinator_name
FROM module_table a
LEFT JOIN subject_table b ON b.id = a.subject_id
LEFT JOIN course_table c ON c.id = a.course_id
LEFT JOIN cordinator_table d ON d.id = a.cordinator_ids
WHERE a.id = $somevalue
ORDER BY a.id DESC
`
Above query is producing error and when I am connecting the two tables Its showing all right
SELECT
a.name as module_name,
a.id,
b.id as subject_id,
b.SUBJECT_name
FROM module_table a
LEFT JOIN subject_table b ON b.id = a.subject_id
WHERE a.id = $somevalue
ORDER BY a.id DESC`
The first Table has all foreign keys for subject and course table, further subject table is connected to coordinator table with common id column.. I want the corresponding names of the id given in the module table..
The last table is the result of query I want from where I can collect my required data
My table structures are below
MODULE CAN BE INCLUDED ONLY IN A SUBJECT AND SUBJECT CAN BE INCLUDED IN COURSE
EACH SUBJECT CAN HAVE ANY NUMBER OF CORDINATORS WHICH I AM KEEPING THEM AS JSON value
-----------------------------------------------------------------------
MODULE TABLE
_____________________________________________________
id | subject_id | course_id | cordinator_id | name
-----------------------------------------------------------------------
COURSE TABLE
__________________
id | course_name
-----------------------------------------------------------------------
SUBJECT TABLE
_________________________________________________
id | course_id | cordinator_id | SUBJECT_name
-----------------------------------------------------------------------
CORDINATOR TABLE
______________________________________
id | cordinator_ids | cordinator_name
Result TABLE
___________________________________________________________________________________________
id | module_name | subject_id | subject_name | course_id | course_name | cordinator_ids
I am able to join two tables successfully with LEFT Join but on third it is reporting an error.
The problem is that you have a few typos and forgot to JOIN course_table.
Here is the right query:
SELECT
a.name as module_name,
a.id,
b.id as subject_id,
b.SUBJECT_name,
c.id as course_id,
c.course_name,
d.id as cordinator_id,
d.cordinator_name
FROM module_table a
LEFT JOIN subject_table b ON b.id = a.subject_id
LEFT JOIN course_table c ON c.id = a.course_id
LEFT JOIN cordinator_table d ON d.id = a.cordinator_id
WHERE a.id = $somevalue
ORDER BY a.id DESC
On this line:
b.SUBJECT_name,
you had to put uppercase "SUBJECT_name" as in your table subject_table schema.
And your query was lacking this JOIN to allow you to select course_table fields:
LEFT JOIN course_table c ON c.id = a.course_id

Categories