How to combine two results? - php

How can I display results from these two queries in same row. Something like a 17/8 (Kills/Deaths).
I have two queries, first:
$result1 = mysql_query("SELECT PlayerTable.Player AS 'Player', COUNT(CASE WHEN PlayerTable.InitiatorGroupCat = 'AIRPLANE' THEN PlayerTable.InitiatorGroupCat END) AS Airplane
FROM
(
SELECT HitTbl.InitiatorPlayer AS 'Player', DeadTbl.InitiatorGroupCat
FROM
(
SELECT *
FROM stats
WHERE (EVENT = 'S_EVENT_PILOT_DEAD')
AND InitiatorGroupCat != ''
) AS DeadTbl
JOIN
(
SELECT *
FROM stats
WHERE EVENT = 'S_EVENT_HIT'
) AS HitTbl
ON DeadTbl.InitiatorID = HitTbl.TargetID
WHERE HitTbl.Time IN
(
SELECT MAX(TIME)
FROM stats
WHERE EVENT = 'S_EVENT_HIT'
GROUP BY TargetID
)
GROUP BY DeadTbl.InitiatorID,DeadTbl.Time
ORDER BY DeadTbl.Time ASC
) AS PlayerTable
GROUP BY PlayerTable.Player
ORDER BY Airplane DESC
");
//return the array and loop through each row
$i = 1;
while ($row2 = mysql_fetch_assoc($result1))
{
?>
<tr valign="top">
<td align=center><?php echo $i ++; ?>.</td>
<td> <?php echo $row2['Player'];?></td>
<td align=center><?php echo $row2['Airplane'];?></td>
</tr>
Second query:
$result2 = mysql_query("SELECT InitiatorPlayer, COUNT(CASE WHEN InitiatorGroupCat = 'AIRPLANE' THEN InitiatorGroupCat END) AS Deaths
FROM stats
WHERE (EVENT = 'S_EVENT_DEAD' OR EVENT = 'S_EVENT_PILOT_DEAD' OR EVENT = 'S_EVENT_EJECTION')
AND InitiatorGroupCat != ''
GROUP BY InitiatorPlayer
ORDER BY Airplane DESC");
Both queries work but I need result from second query display in row from first in form:
1.| Player | 17/8 (8 is result from second query)
This is the combination (new column in first query divided by "/"):
<?php echo $row2['Airplane']. " / ".$row2['Deaths'];?>

Solved in this way:
<?php
$result1 = mysql_query("SELECT PlayerTable.Player AS 'Player', COUNT(CASE WHEN PlayerTable.InitiatorGroupCat = 'AIRPLANE' THEN PlayerTable.InitiatorGroupCat END) AS Airplane
FROM
(
SELECT HitTbl.InitiatorPlayer AS 'Player', DeadTbl.InitiatorGroupCat
FROM
(
SELECT *
FROM stats
WHERE (EVENT = 'S_EVENT_PILOT_DEAD' OR EVENT = 'S_EVENT_DEAD' )
AND InitiatorGroupCat != ''
) AS DeadTbl
JOIN
(
SELECT *
FROM stats
WHERE EVENT = 'S_EVENT_HIT'
) AS HitTbl
ON DeadTbl.InitiatorID = HitTbl.TargetID
WHERE HitTbl.Time IN
(
SELECT MAX(TIME)
FROM stats
WHERE EVENT = 'S_EVENT_HIT'
GROUP BY TargetID
)
GROUP BY DeadTbl.InitiatorID,DeadTbl.Time
ORDER BY DeadTbl.Time ASC
) AS PlayerTable
GROUP BY PlayerTable.Player
ORDER BY Airplane DESC
");
$result2 = mysql_query("SELECT InitiatorPlayer, COUNT(CASE WHEN InitiatorGroupCat = 'AIRPLANE' THEN InitiatorGroupCat END) AS Deaths
FROM stats
WHERE (EVENT = 'S_EVENT_PILOT_DEAD' OR EVENT = 'S_EVENT_DEAD')
AND InitiatorGroupCat != ''
GROUP BY InitiatorPlayer");
//return the array and loop through each row
//return the array and loop through each row
$i = 1;
while (($row2 = mysql_fetch_array($result1)) && ($row1 = mysql_fetch_array($result2)))
{
?>
<tr valign="top">
<td align=center><?php echo $i ++; ?>.</td>
<td> <?php echo $row2['Player'];?></td>
<td align=center><?php echo $row2['Airplane']. " / " .$row1['Deaths'];?></td>
</tr>
This is a key:
while (($row2 = mysql_fetch_array($result1)) && ($row1 = mysql_fetch_array($result2)))

Related

How can I implement this SQL query using CodeIgniter Query Builder?

I am a beginner using codeigniter query builder class. How can I do this SQL in Query builder Codeigniter..?
Any help is appreciated. Thanks!
SET #interval = 15;
SET #store = 1;
SET #id = 0;
SET #start_dt = '2022-10-12 13:30:00';
SET #end_dt = '2022-10-12 22:30:00';
SELECT
mrt.shoptype_code,
DATE_ADD(#start_dt, INTERVAL vt1.num MINUTE) AS v_dt,
(
SELECT count(*)
FROM s_reservation
WHERE store_code = #store AND id <> #id AND shop_type = mrt.shoptype_code AND status=1 AND start_time <= v_dt AND v_dt < end_time
) AS reservations
FROM
(
SELECT #num := 0 AS num UNION
SELECT #num := #num + #interval FROM information_schema.COLUMNS
) AS vt1
CROSS JOIN
m_shoptype AS mrt
WHERE mrt.store_code = #store
HAVING v_dt BETWEEN #start_dt AND #end_dt
Here I'm trying...
$this->db->SELECT('mrt.shoptype_code AS reservations');
$this->db->DATE_ADD('#start_dt', 'INTERVAL vt1.num MINUTE');
$this->db->SELECT('count(*)');
$this->db->where('store_code = #store AND id <> #id AND shop_type = mrt.shoptype_code AND status=1 AND start_time <= v_dt AND v_dt < end_time');
$this->db->join('m_shoptype as mrt','true');
$result = $this->db->get('s_reservation as reservations')->result();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "Status " . $row["reservations"]. "<br>";
}
}
else {
echo "Na data found..! ";
}

Calculate SUM in PHP

<?php
$today=date('Y-m-d');
$sorgu = mysqli_query($conn, "SELECT * FROM urun");
while ($sira = mysqli_fetch_array($sorgu)) {
$urun = $sira['urun'];
$query = mysqli_query($conn, "SELECT product_name, birim, SUM(quantity) FROM
product WHERE product_name = '$urun' AND quantity != '0' AND quantity > '0'
AND grup!='uygulama' AND skt > '$today' GROUP BY product_name, birim ");
while ($row = mysqli_fetch_array($query)){
if ($row['2'] <= $sira['minstok']) {
echo count($row['0']);
} } } ?>
This code belongs to a table which has a notification system. I want the notification part increase when the line number increases in that table but it turns 11111111... , So how can I make the code give me sum?
Incidentally, everything up to while ($row... can be rewritten as follows:
SELECT p.product_name
, p.birim
, SUM(p.quantity) total
FROM urun u
JOIN product p
ON p.product_name = u.urun
AND p.quantity > 0
AND p.grup != 'uygulama'
AND p.skt > CURDATE()
GROUP
BY p.product_name
, p.birim;

Display percentage from two sql columns

This is a small section of code i've been working with.
<?php
$rank = 0;
$rank = 0;
$sql = mysql_query("SELECT m.memberID, username,email,target,type, ifnull(t.total,0) as total, ifnull(m.total,0) as totalp FROM `members` as m
left join (
select s.memberID, sum(amount) total from sponsors as s group by s.memberID
) t on t.memberid = m.memberid
left join (
select s.memberID, sum(amount) total from sponsors as s group by s.memberID
) p on p.memberid = m.memberid Where type= 'business'
ORDER BY t.total DESC, p.total DESC, username ASC");
while($row = mysql_fetch_object($sql))
{
echo "<tr>";
$rank = $rank + 1;
echo "<td><center>$rank</center></td>";
echo "<td>$row->username</td>";
echo "<td>$row->total</td>";
echo "<td>$row->target</td>";
echo "<td>$row->here i want percentage</td>";
echo "</tr>";
}
?>
What I would like to do is display the percentage of total/target in the final column of my table. Not sure what the best way to do this is and would be keen to hear some ideas for this.
I did try doing it just as total/target in the echo but thinking it probably needs to be evaluated earlier as a variable or something? Whatever i tried didn't work anyway...
Looked at this but not sure how to implement it in my scenario - MSSQL display percentage calculated from two columns
Try this,
<?php
$rank = 0;
$rank = 0;
$sql = mysql_query("SELECT m.memberID, username,email,target,type, ifnull(t.total,0) as total, ifnull(m.total,0) as totalp, (ifnull(t.total,0)/target)*100 as present FROM `members` as m
left join (
select s.memberID, sum(amount) total from sponsors as s group by s.memberID
) t on t.memberid = m.memberid
left join (
select s.memberID, sum(amount) total from sponsors as s group by s.memberID
) p on p.memberid = m.memberid Where type= 'business'
ORDER BY t.total DESC, p.total DESC, username ASC");
while($row = mysql_fetch_object($sql))
{
echo "<tr>";
$rank = $rank + 1;
echo "<td><center>$rank</center></td>";
echo "<td>$row->username</td>";
echo "<td>$row->total</td>";
echo "<td>$row->target</td>";
echo "<td>$row->present</td>";
echo "</tr>";
}
?>
Here is logic
((cast("A/B" as float)/"C") * 100.00) AS "D%"
all value must be integer and when you are dividing them it must be convert as a float so calculation goes correctly.
Hope it will work for you.

How do I get the result into a variable MYSQL

I have the following PHP script. How do I get the correct results. Each query should only return a number. I need to get rank in this case. I was also wondering how to combine those two queries into one statement.
function getRankings($country, $deviceid)
{
$queryWorld = "SELECT 1 + (SELECT count( * ) FROM ScoreTable a WHERE a.score > b.score ) AS rank FROM
ScoreTable b WHERE DeviceID='$deviceid' ORDER BY rank LIMIT 1";
$queryCountry = "SELECT 1 + (SELECT count( * ) FROM ScoreTable a WHERE a.score > b.score AND Country='$country') AS rank FROM ScoreTable b WHERE DeviceID='$deviceid' ORDER BY rank LIMIT 1";
$resultWorld = mysql_query($queryWorld) or die(mysql_error());
$rowWorld = mysql_fetch_row($resultWorld);
$resultCountry = mysql_query($queryCountry) or die(mysql_error());
$rowCountry = mysql_fetch_row($resultCountry);
$arr = array();
$arr[] = array("WorldRanking" => $rowWorld[0], "CountryRanking" => $rowCountry[0]);
echo json_encode($arr);
}
If I type the queries individually into MYSQl I get the correct answers. But the echo produces
[{"WorldRanking":null,"CountryRanking":null}]
It should be something like
[{"WorldRanking":"4","CountryRanking":"1"}]
I think I need to get the value of rank but I do not know how.
try your code like this, but I don't know this code correct or not:
function getRankings($country, $deviceid)
{
$queryWorld = "SELECT 1 + (SELECT count( * ) FROM ScoreTable a WHERE a.score > b.score ) AS rank FROM
ScoreTable b WHERE DeviceID='$deviceid' ORDER BY rank LIMIT 1";
$queryCountry = "SELECT 1 + (SELECT count( * ) FROM ScoreTable a WHERE a.score > b.score AND Country='$country') AS rank FROM ScoreTable b WHERE DeviceID='$deviceid' ORDER BY rank LIMIT 1";
$resultWorld = mysql_query($queryWorld) or die(mysql_error());
while ($rowWorld = mysql_fetch_row($resultWorld)){
$value1 = $rowWorld['filedname1'];
}
$resultCountry = mysql_query($queryCountry) or die(mysql_error());
while($rowCountry = mysql_fetch_row($resultCountry)){
$value2 = $rowWorld['filedname2'];
}
$arr[] = array("WorldRanking" => $value1, "CountryRanking" => $value2);
print_r($arr);
}
<?php
function getRankings($country, $deviceid)
{
$queryWorld = "SELECT 1 + (SELECT count( * ) FROM ScoreTable a WHERE a.score > b.score ) AS rank FROM
ScoreTable b WHERE DeviceID=$deviceid ORDER BY rank LIMIT 1";
$queryCountry = "SELECT 1 + (SELECT count( * ) FROM ScoreTable a WHERE a.score > b.score AND Country='$country') AS rank FROM ScoreTable b WHERE DeviceID=$deviceid ORDER BY rank LIMIT 1";
$resultWorld = mysql_query($queryWorld) or die(mysql_error());
$no_of_results_in_resultWorld = mysql_num_rows($resultWorld);
if($no_of_results_in_resultWorld > 0){
$rowWorld = mysql_fetch_row($resultWorld);
}
$resultCountry = mysql_query($queryCountry) or die(mysql_error());
$no_of_results_in_resultCountry = mysql_num_rows($resultCountry);
if($no_of_results_in_resultCountry > 0){
$rowCountry = mysql_fetch_row($resultCountry);
}
$arr = array();
$arr[] = array("WorldRanking" => $rowWorld[0], "CountryRanking" => $rowCountry[0]);
echo json_encode($arr);
}
check whether it is returning results or not.

MySQL query with one variable

I have a table called 'main_table' with 3 columns :
'player', 'points' and 'drop_date'
I have 1 variable ($date) with different values:
$date == '2012-06-01'
$date == '2012-05-01'
$date == '2012-04-01'
I Have 1 MySQL query:
$query = "
select *
from main_table
where `drop_date` > '$date'
AND `drop_date` <= DATE_ADD('$date', INTERVAL 1 YEAR)
LIMIT 1
";
GOAL :
I would like to run ONE query with different passes (1pass per value)
I have tried :
<?php
$date['date'] = '2012-06-01';
$date['date'] = '2012-05-01';
$date['date'] = '2012-04-01';
foreach($date as $title => $actual_date) {
query = "
select *
from main_table
where `drop_date` > '$actual_date'
AND `drop_date` <= DATE_ADD('$actual_date', INTERVAL 1 YEAR)
LIMIT 1
";
$result = mysql_query($query) or die(mysql_error());
}
while($row = mysql_fetch_array($result)) {
echo $row['Player'];
echo $row['Points'];
}
You keep overwriting the same variable over and over... and then you run the query but only fetch results for the last one. How do you expect it to work?
Try this:
$date = Array("2012-06-01","2012-05-01","2012-04-02");
foreach($date as $actual_date) {
if( $result = mysql_fetch_assoc(mysql_query("select * from `main_table` where `drop_date`>'".$actual_date."' and `drop_date`<=date_add('".$actual_date."',interval 1 year) limit 1"))) {
echo $result['Player'];
echo $result['Points'];
}
}
Note that I skipped putting the query in a variable, and putting the query result in a variable, and just one-lined the whole thing. Since you have limit 1 the query will only return one row, so there is no need to while-loop it.
Just move your while in the foreach loop.
<?php
$date[0] = '2012-06-01';
$date[1] = '2012-05-01';
$date[2] = '2012-04-01';
foreach($date as $title => $actual_date)
{
query = "select * from main_table where `drop_date` > '$actual_date' AND `drop_date` <= DATE_ADD('$actual_date', INTERVAL 1 YEAR) LIMIT 1";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['Player'];
echo $row['Points'];
}
}
Do not overwrite your values. Do this to keep all 3 values.
<?php
$date['date'][] = '2012-06-01';
$date['date'][] = '2012-05-01';
$date['date'][] = '2012-04-01';
print_r($date);
?>
OUTPUT:
Array
(
[date] => Array
(
[0] => 2012-06-01
[1] => 2012-05-01
[2] => 2012-04-01
)
)
Then use
foreach ($date['date'] as $actual_date) {
$query = "
select *
from main_table
where `drop_date` > '$actual_date'
AND `drop_date` <= DATE_ADD('$actual_date', INTERVAL 1 YEAR)
LIMIT 1";
echo $query."<br />";
}
OUTPUT:
select *
from main_table
where `drop_date` > '2012-06-01'
AND `drop_date` <= DATE_ADD('2012-06-01', INTERVAL 1 YEAR)
LIMIT 1
<br />
select *
from main_table
where `drop_date` > '2012-05-01'
AND `drop_date` <= DATE_ADD('2012-05-01', INTERVAL 1 YEAR)
LIMIT 1
<br />
select *
from main_table
where `drop_date` > '2012-04-01'
AND `drop_date` <= DATE_ADD('2012-04-01', INTERVAL 1 YEAR)
LIMIT 1
<br />

Categories