Converting PHP Process to MySQL - php

select c.kupon, count(*) as count
from kuponbahis c
join bahis b
on b.sonuc = c.secim
and b.ID = c.bahis
group by c.kupon
having count(case when c.bahis ='$sonuclandirilacakbahis' then 1 end) > 0
With this query I'm getting kupon IDs and counts. Then fetching them in PHP and matching with the result of
SELECT COUNT(*) FROM kuponbahis WHERE kupon='$kuponid'
(while fetching the second query, too.) If it's a match, i'm doing some work.
But now, I want to do this in SQL directly.
My PHP code is below;
$kazananKuponlariGetirSorgu = mysql_query("select c.kupon, count(*) as count
from kuponbahis c
join bahis b
on b.sonuc = c.secim
and b.ID = c.bahis
group by c.kupon
having count(case when c.bahis ='$sonuclandirilacakbahis' then 1 end) > 0");
while ($kazananKuponlariGetirSorgux = mysql_fetch_array($kazananKuponlariGetirSorgu)){
$kuponid = $kazananKuponlariGetirSorgux[0];
$tutanbahisadet = $kazananKuponlariGetirSorgux[1];
$kupondakiBahisAdeti = mysql_query("SELECT COUNT(*) FROM kuponbahis WHERE kupon='$kuponid'");
$kupondakiBahisAdetix = mysql_fetch_array($kupondakiBahisAdeti);
if ($kupondakiBahisAdetix[0]==$tutanbahisadet){
//it's a match
}
I tried many queries but they all failed. How can I merge this two sql process to only one?

The obvious way is in the having clause:
select c.kupon, count(*) as count
from kuponbahis c join
bahis b
on b.sonuc = c.secim and b.ID = c.bahis
group by c.kupon
having sum(c.bahis = '$sonuclandirilacakbahis') > 0 and
count(*) = (SELECT COUNT(*) FROM kuponbahis WHERE kupon = '$kuponid');

Related

UNION ALL use with INNER JOIN

I am trying to join 3 tables with UNION ALL. I tried the following code. and giving this error of Invalid parameter number: number of bound variables does not match number of tokens.
$codeArray = explode(',', $code);
$inQuery = implode(',', array_fill(0, count($codeArray), '?'));
$full_dt = date('Y-m-d H:i:s');
$query = "SELECT * FROM
(
SELECT
a.*
FROM pat_info a
INNER JOIN
pat_medication b
ON a.id = b.pat_id
WHERE
a.status != 2 AND b.status != 2
AND '$full_dt' BETWEEN b.start_date AND b.end_date
AND a.location_code IN ($inQuery)
AND b.stock_status != '2'
AND (b.total_qty - (b.given + b.not_taken)) < 12
UNION ALL
SELECT
a.*
FROM pat_info a
INNER JOIN
prn_medication b
ON a.id = b.pat_id
WHERE
a.status != 2 AND b.status != 2
AND '$full_dt' BETWEEN b.start_date AND b.end_date
AND a.location_code IN ($inQuery)
AND b.stock_status != '2'
AND (b.total_qty - (b.given + b.not_taken)) < 12
) x
GROUP BY a.id ORDER BY a.id DESC";
$statement = $con->prepare($query);
$statement->execute($codeArray);
As you have the in clause twice in your code, you need to bind the values twice.
A simple way to do this would be to duplicate the data prior to the execute()...
$codeArray = array_merge($codeArray, $codeArray);
You also need to change
GROUP BY a.id ORDER BY a.id DESC
to
GROUP BY x.id ORDER BY x.id DESC
as the a alias is in the sub-select and not the overall SELECT.

sql calculating query doesn't run, #1054 unknown column in field list

I want to create query that would show is the certain section is passed by certain account_id. It means all lessons of the section is checked = 1(true). I tried this solution:
SELECT count(*) as checked,(
SELECT count(*)
FROM lessons
WHERE section_id = 1
GROUP BY section_id
) as cnt, (checked = cnt) as passed
FROM lessons l
LEFT JOIN progress p ON l.id = p.lesson_id
WHERE l.section_id = 1 AND p.account_id = 3 AND checked = 1
GROUP BY l.section_id
But it returns error:
#1054 unknown 'cnt' column in field list.
What do I do wrong?
The unknown 'cnt' is generated from (checked=cnt) as passed. Try this
SELECT checked, cnt, (checked=cnt) as passed FROM (SELECT count(*) as checked,(
SELECT count(*)
FROM lessons
WHERE section_id = 1
GROUP BY section_id
) as cnt
FROM lessons l
LEFT JOIN progress p ON l.id = p.lesson_id
WHERE l.section_id = 1 AND p.account_id = 3 AND checked = 1
GROUP BY l.section_id) tblA
Note that you should move all conditions on the p.* columns to the ON clause. Otherwise you will convert the LEFT JOIN to an INNER JOIN and COUNT(*) will always be the same as in your subquery. However you don't even need that subquery - You can get the same value with COUNT(p.lesson_id) instead. It will ignore all rows with NULL.
SELECT
COUNT(*) as cnt,
COUNT(p.lesson_id) as checked,
COUNT(*) = COUNT(p.lesson_id) as passed
FROM lessons l
LEFT JOIN progress p
ON p.lesson_id = l.id
AND p.account_id = 3
AND p.checked = 1
WHERE l.section_id = 1
SELECT count(*) as checked, (SELECT count(*) FROM lessons
WHERE section_id = 1 GROUP BY section_id) as cnt,
((SELECT count(*) FROM lessons WHERE section_id = 1 GROUP BY section_id) = checked) pass
FROM lessons l LEFT JOIN progress p ON l.id = p.lesson_id
WHERE l.section_id = 1 AND p.account_id = 3 AND checked = 1 GROUP BY l.section_id
Thanks to #aynber. Though I'm not sure if this solution isn't too bulky.

Return rows and total count of returned rows in ORACLE, PHP

I have SQL query:
$query = "SELECT DISTINCT f.ID, b.NAME, a.USER_ID, b.PARENT_ID
FROM b_disk_simple_right s, b_user_access a, b_disk_object b, b_file f
WHERE s.ACCESS_CODE = a.ACCESS_CODE
AND a.USER_ID = ".$userID."
AND b.CREATE_TIME > trunc(sysdate)-200
AND s.OBJECT_ID = b.ID
AND f.ID = b.FILE_ID
AND b.DELETED_BY=0";
global $DB;
$res=$DB->Query($query,true);
I need to get total count of returned rows. I am not able to use oci_num_rows function. How to do this?
Below one will gives you the number of rows count
"select count(*) as count
from
(
SELECT DISTINCT f.ID, b.NAME, a.USER_ID, b.PARENT_ID
FROM b_disk_simple_right s, b_user_access a, b_disk_object b, b_file f
WHERE s.ACCESS_CODE = a.ACCESS_CODE
AND a.USER_ID = ".$userID."
AND b.CREATE_TIME > trunc(sysdate)-200
AND s.OBJECT_ID = b.ID
AND f.ID = b.FILE_ID
AND b.DELETED_BY=0
)"

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 Query with IF statements

having a little trouble, how could I alter this mysql query so that if there are rows in the member_results table it will run the query below but if there is none then pretty much run the query below just without the SUMS() and the joins with the member_results table. I am guessing it can be done with the IF statement in mysql I just have no clue really how to implement with the query below.
Any help is much appreciated.
$result = mysql_query(" SELECT m.member_id, m.teamname,
Sum(Case When r.track_id = '$chosentrack' -1 AND r.track_id >= l.start_race
Then total_points Else 0 End) LastRacePoints,
Sum(Case When r.track_id <= '$chosentrack' -1 AND r.track_id >= l.start_race
Then total_points Else 0 End) TotalPoints
FROM members m
Join members_leagues l
On l.member_id = m.member_id
Join member_results r
On r.member_id = m.member_id
Where l.league_id = '$chosenleague'
Group By m.member_id
Order By TotalPoints Desc, LastRacePoints DESC, m.teamname Desc ")
or die ("Error - could not display league");
Just replace Join member_results r with Left Join member_results r and it should work. You should get NULLs for the sums if there are no results.

Categories