Query two tables together, only one record displaying - php

I am building a polling system and I have a query right now that is supposed to select all of the polls and count all of the votes for each of the polls (which lie in a separate table). So my tables look like:
Polls:
ID
Title
Body
Votes:
ID
PollID
Vote (This value is either 0 or 1)
Well the totaling of the votes looks like it is working, the issue is that it is currently only displaying one record.
Currently my query looks like this:
SELECT POLLS.ID,
POLLS.TITLE,
POLLS.BODY,
Sum(CASE
WHEN VOTES.VOTE = 1
AND VOTES.POLLID = POLLS.ID THEN 1
ELSE 0
END) AS yay,
Sum(CASE
WHEN VOTES.VOTE = 0
AND VOTES.POLLID = POLLS.ID THEN 1
ELSE 0
END) AS nay,
FROM polls,
VOTES
ORDER BY POLLS.ID
Also I am using PHP with Codeigniter.

SELECT polls.ID,polls.title,polls.body,
SUM(case when votes.vote = 1 then 1 else 0 end) AS yay,
SUM(case when votes.vote = 0 then 1 else 0 end) AS nay,
FROM polls
JOIN votes ON polls.ID = votes.pollID
GROUP BY poll.ID, polls.title, polls.body
ORDER BY polls.ID

You need to GROUP BY pollID the values in the votes table:
SELECT p.ID, p.title, p.body, v.yay, v.nay
FROM
(
SELECT PollID,
SUM(CASE WHEN vote = 1 THEN 1 ELSE 0 END) AS yay,
SUM(CASE WHEN vote = 0 THEN 1 ELSE 0 END) AS nay,
FROM votes
GROUP BY PollID
) v
INNER JOIN Polls p ON v.PollID = p.Id
ORDER BY p.ID

I'd go with:
SELECT polls.ID, polls.title, polls.body,
SUM(IF(votes.vote = 1, 1, 0)) AS yay,
SUM(IF(votes.vote = 0, 1, 0)) AS nay
FROM polls
JOIN votes ON (votes.pollID = polls.ID)
GROUP BY ID, title, body

What you are missing is "GROUP BY". Use GROUP BY PollID while taking sum.

Related

SELECT COUNT(DISTINCT column) doesn't work

Why DISTINCT UID doesn't work in my code below?
$q = mysql_fetch_array(mysql_query("SELECT COUNT(DISTINCT UID) AS TOTAL,
SUM(CASE WHEN SYSTEM = 'Android' THEN 1 ELSE 0 END) AS A,
SUM(CASE WHEN SYSTEM = 'IOS' THEN 1 ELSE 0 END) AS I,
SUM(CASE WHEN SYSTEM = 'Windows' THEN 1 ELSE 0 END) AS W FROM user_visits"));
The query returns all items from the database selected even I put DISTINCT UID or *. Same result.
The distinct keyword is supposed to be outside like below,
SELECT DISTINCT column1, column2, ...
FROM table_name;
?
Also, you are trying to sum few things, It should be something like below,
SELECT UID, COUNT(UID) AS TOTAL,
SUM(CASE WHEN SYSTEM = 'Android' THEN 1 ELSE 0 END) AS A,
SUM(CASE WHEN SYSTEM = 'IOS' THEN 1 ELSE 0 END) AS I,
SUM(CASE WHEN SYSTEM = 'Windows' THEN 1 ELSE 0 END) AS W FROM user_visits
GROUP BY UID
with aggregate (group by)

Mysql query not working for me Codeigniter Php

I am working with mysql with codeigniter, hHere is my table structure:
Table A:
id(doctorid) name
-----------------------------
1 abc
2 xyz
3 ahd
4 djd
Table B:
id doctor_id type
-------------------------------------
1 1 Nuter
2 3 Spay
Now I want to get all records of doctor with count of type, I want the following result:
id name Nuter Spay
---------------------------------------
1 abc 1 0
2 xyz 0 Spay
I tried with following code but not working for me, how can I do this ?
$this->db->select('A.*');
$this->db->from('A');
$this->db->join('B', 'B.doctor_id = (SELECT COUNT(Type) FROM B )');
$query = $this->db->get();
Try this code:
$this->db->select('A.*,B.type');
$this->db->from('A');
$this->db->join('B', 'B.doctor_id = A.id');
$query = $this->db->get();
It seems like you are looking for conditional aggregation.
In pure SQL, the query should look like:
SELECT
A.id,
A.name,
SUM(CASE WHEN B.type = 'Nuter' THEN 1 ELSE 0 END) as Nuter,
SUM(CASE WHEN B.type = 'Spay' THEN 1 ELSE 0 END) as Spay
FROM A
INNER JOIN B ON B.doctor_id = A.id
GROUP BY A.id, A.name
If you have a 1-1 relationship between tables A and B instead of 1-N (as your sample data suggests), then this can be simplified as:
SELECT
A.id,
A.name,
CASE WHEN B.type = 'Nuter' THEN 1 ELSE 0 END as Nuter,
CASE WHEN B.type = 'Spay' THEN 1 ELSE 0 END as Spay
FROM A
INNER JOIN B ON B.doctor_id = A.id
Try:
$this->db->select(
"A.id,
A.name,
CASE WHEN B.type = 'Nuter' THEN 1 ELSE 0 END as Nuter,
CASE WHEN B.type = 'Spay' THEN 1 ELSE 0 END as Spay"
);
$this->db->from('A');
$this->db->join('B', 'B.doctor_id = A.id');
$query = $this->db->get();

Mysql inner join on 2 tables don't work

I have a table in my database named contacts and a table named views.
On the table contacts I have the following fields:
id
status
first_name
last_name
The status can be cold, prospect or lost.
On the table views I have the following fields:
user_id
art_views
art_title
The relation between those 2 tables is id and user_id.
I need a query to make a new html table with the following columns:
art_title
cold
prospect
lost
Now I have the following query (UPDATED):
SELECT
v.art_title,
SUM(CASE c.status WHEN 'cold' THEN v.art_views ELSE 0 END) cold,
SUM(CASE c.status WHEN 'prospect' THEN v.art_views ELSE 0 END) prospect,
SUM(CASE c.status WHEN 'lost' THEN v.art_views ELSE 0 END) lost
FROM views v
JOIN contacts c ON v.user_id = c.id
GROUP BY v.art_title
This query is working now (thanks to Gerv) but i still have users who don't have a status. So i leave the field user_id in the table 'views' empty. How can i change the query for those users so i can count them also?
I tried to: SUM(CASE v.user_id WHEN ' ' THEN v.art_views ELSE 0 END) test,
but with no result here.
You can switch the logic by selecting from the views table and joining de contacts table.
Below query will pivot the status with a CASE clause
SELECT
v.art_title,
SUM(CASE c.status WHEN 'cold' THEN v.art_views ELSE 0 END) cold,
SUM(CASE c.status WHEN 'prospect' THEN v.art_views ELSE 0 END) prospect,
SUM(CASE c.status WHEN 'lost' THEN v.art_views ELSE 0 END) lost,
SUM(CASE c.status WHEN NULL THEN v.art_views ELSE 0 END) no_user
FROM views v
LEFT JOIN contacts c ON v.user_id = c.id
GROUP BY v.art_title
ORDER BY (cold+lost+prospect+no_user) DESC
LIMIT 10
Try
SELECT art_title, SUM(art_views), status FROM contacts AS c INNER JOIN views AS v ON v.user_id = c.id GROUP BY v.art_title, c.status

How to count records as per all year?

I need to count record year wise, I did some query but i am not getting correct result. Below is my query. But that is not working for me. Can anyone please look in this and give me right query ? Any help will be appreciated.
SELECT
(SELECT count(DISTINCT id) FROM call_response WHERE disposition=0 AND user_id=pu.id ) AS `trueAlarm`,
(SELECT count(DISTINCT id) FROM call_response WHERE disposition=1 AND user_id=pu.id ) AS `falseAlarm`,
(SELECT count(DISTINCT id) FROM call_response WHERE disposition=2 AND user_id=pu.id ) AS `disregarded`,
YEAR(cr.created_date) AS `callYear`
FROM `call_response` AS `cr`
INNER JOIN `permit_users` AS `pu`
ON cr.user_id=pu.id
WHERE ( pu.is_deleted=0 AND pu.is_trashed=0 AND cr.is_deleted=0)
GROUP BY `callYear`
The query that you want uses either conditional aggregation or subqueries, but not both. In other words, either use the subqueries but do not have an outer join to call_response. Or, have the outer join but not the subqueries.
I would write the query like this:
SELECT count(distinct case when disposition = 0 AND user_id = pu.id then id end) as trueAlarm,
count(distinct case when disposition = 1 AND user_id = pu.id then id end) as falseAlarm,
count(distinct case when disposition = 2 AND user_id = pu.id then id end) as disregarded,
YEAR(cr.created_date) AS `callYear`
FROM `call_response` `cr` INNER JOIN
`permit_users` `pu`
ON cr.user_id = pu.id
WHERE pu.is_deleted = 0 AND pu.is_trashed = 0 AND cr.is_deleted = 0
GROUP BY `callYear`;

Mysql in conditon related query

SELECT ac1.id, ac1.course_name, case when a.yettoapprove is not null
then 'true'
else 'false'
end OrderedAll , a.* from (SELECT ac.id, sum(case when ad.status = '2' then 1 else 0 end) as yettoapprove , sum(case when ad.status = '1' then 1 else 0 end) as approved,case when ad.status = '2' is not null
then 'true'
else 'false'
end OrderedAll FROM aw_ou_student_data ad , jos_users ju , aw_ou_lookup_courses ac,aw_ou_lookup_colleges ac1 where ac1.id=ju.college_code and ac.id in (27,28,29,30,133,32,33,34,35,36,37,38,39,40,41,42,43,44,134,135) and ac.school_id=2 and ju.id=ad.user_id and ac.id=ju.course_code GROUP BY ac.id ORDER BY ac.id ) a left join aw_ou_lookup_courses ac1 on ac1.id = a.id
In the above inner query in above one i have given 20 ac.id but i am getting only 14 out records as a result. How do i get the total 20 records as a result. i.e The id present in the in condition if it doesnt satisfy then i should a record with 0 as values for it beside that id.
How do i do it..?
A simple left join with condition should do it:
SELECT
table1.id,
table2.*
FROM
table table1
LEFT JOIN table2 ON table1.id = table2.id AND (your conditions here)

Categories