RSS Feed MySQL - Not displaying lowest MYSQL ID on GROUP - php

I am trying to create a RSS Script and have some Problem showing the lowest ID of nNr to show cPath (image)
MySQL Database seems like here
tartikel
+----------+-------+---------------------+------------+-------+
| kArtikel | cName | cKurzBeschreibung | dErstellt | cSeo |
+----------+-------+---------------------+------------+-------+
| 560 | 12345 | Short Description | 2014-03-25 | 12345 |
+----------+-------+---------------------+------------+-------+
| 561 | ABCDE | Short Description | 2014-03-25 | abcde |
+----------+-------+---------------------+------------+-------+
tartikelpict
+--------------+--------------+-------+
| kartikelpict | cPfad | nNr |
+--------------+--------------+-------+
| 560 | picture4.jpg | 4 |
| 560 | picture3.jpg | 3 |
| 560 | picture2.jpg | 2 |
| 560 | picture.jpg | 1 |
+--------------+--------------+-------+
| 561 | picture4.jpg | 4 |
| 561 | picture3.jpg | 3 |
| 561 | picture2.jpg | 2 |
| 561 | picture.jpg | 1 |
+--------------+--------------+-------+
My PHP Code
$result = mysql_query('SELECT
p1.kArtikel,
p1.cName,
p1.cKurzBeschreibung,
p1.dLetzteAktualisierung,
p1.dErstellt,
p1.cSeo,
p2.kartikelpict,
p2.nNr,
p2.cPfad
FROM tartikel AS p1
JOIN tartikelpict AS p2
ON (p1.kArtikel = p2.kartikelpict)
JOIN (SELECT kartikelpict ,MIN(nNr) nNr FROM tartikelpict GROUP BY kartikelpict ) p3
ON(p2.kartikelpict = p3.kartikelpict AND p2.nNr = p3.nNr)
ORDER BY p1.kArtikel
DESC LIMIT 50', $connection);
while ($row = mysql_fetch_array($result)){
$cName = $row['cName'];
$cKurzBeschreibung = $row['cKurzBeschreibung'];
$dLetzteAktualisierung = $row['dLetzteAktualisierung'];
$dErstellt = $row['dErstellt'];
$cSeo = $row['cSeo'];
$date = strtotime($row['dErstellt']);
$pubdate = date(r, $date);
$id = $row['kArtikel'];
$nNr = $row['nNr'];
$cPfad = $row['cPfad'];
echo "\t\t" . '<enclosure url="'.$cPath.'" type="image/jpeg" />' . "\n";
The Result (it should be only the lowest)
<enclosure url="4" />
<enclosure url="3" />
<enclosure url="2" />
<enclosure url="1" />
I think I can't build the logic to get the right element. I have to get the ID of nNr and than select cPfad right? What am I doing wrong?
The SQL and Table seems to be OK after tested #sqlfiddle (thx to Khalid). Any idea what is wrong on my Code?

Related

Query SUM for two fields in two different tables PHP

I am having a problem generating the result I need. I want to sum the other table gg_hp base on gg_id
+--------------+----------+-------+------------+
| sg_name | sg_grade | gg_id | subject_id |
+--------------+----------+-------+------------+
| Quiz 1 | 20 | 14 | 68 |
| Midterm Exam | 50 | 15 | 68 |
| Quiz 2 | 50 | 14 | 68 |
+-------+--------------+----------+-------+----+
tbl_gradesubcateg
+-------+--------------+------------+------------+------------+------------+-------+
| gg_id | categname | percentage | gradecount | teacher_id | subject_id | gg_hp |
+-------+--------------+------------+------------+------------+------------+-------+
| 14 | Quiz | 10 | NULL | 4 | 68 | 0 |
| 15 | Midterm Exam | 20 | NULL | 4 | 68 | 0 |
+-------+--------------+------------+------------+------------+------------+-------+
This is my expected output
+-------+--------------+------------+------------+------------+------------+-------+
| gg_id | categname | percentage | gradecount | teacher_id | subject_id | gg_hp |
+-------+--------------+------------+------------+------------+------------+-------+
| 14 | Quiz | 10 | NULL | 4 | 68 | 70 |
| 15 | Midterm Exam | 20 | NULL | 4 | 68 | 50 |
+-------+--------------+------------+------------+------------+------------+-------+
This is the query I made but.. Im not getting accurate result.
$querycount = "SELECT * FROM tbl_gradesubcateg order by gg_id asc ";
$query_run = mysqli_query($con,$querycount);
$sums= 0;
$ctr = 0;
$id1 = "";
while ($num = mysqli_fetch_assoc ($query_run)) {
$sums += $num['sg_grade'];
$id= $num['gg_id'];
if($id == $id1)
{
$queryhp = mysqli_query($con,"UPDATE tbl_gradecateg SET gg_hp = '".$sums."' where gg_id='".$id."'") or die(mysqli_error($con));
}
else
{
$queryhp = mysqli_query($con,"UPDATE tbl_gradecateg SET gg_hp = '".$sums."' where gg_id='".$id."'") or die(mysqli_error($con));
$sums= 0;
}
$id1= $num['gg_id'];
}
```
Any thoughts would be great.
A correlated subquery is a simple approach:
update tbl_gradesubcateg gs
set sg_grade = (select sum(sg.g_grade)
from gg_hp g
where g.gg_id = gs.gg_id
);
I don't recommend doing this calculation, though. It is simple enough to aggregate when you query the table. Summarizing the results just means that they are out-of-date the next time rows are inserted, updated, or deleted in the tables.

sql query is taking long time to execute

I wrote this sql query and it works but it is taking long time to be executed
SELECT trans_files.id, trans_files.game_name, trans_files.file_name,
COUNT(en_txt.id) as txt_num, COUNT(ar_txt.id) as n_txt_num
FROM trans_files
LEFT JOIN en_txt ON en_txt.file_id = trans_files.id
LEFT JOIN ar_txt ON ar_txt.en_text_id = en_txt.id
&& ar_txt.date = (SELECT MAX(ar_txt.date)
FROM ar_txt
WHERE en_text_id = en_txt.id)
GROUP BY trans_files.id
ORDER BY trans_files.id DESC, trans_files.game_name ASC, trans_files.file_name ASC
..
mysql> select * from ar_txt limit 3;
+----+------------+---------+----------------+---------------------+
| id | en_text_id | user_id | text | date |
+----+------------+---------+----------------+---------------------+
| 1 | 16 | 3 | Ϻ┘äÏ▓Ï╣┘è┘à | 2017-01-24 19:10:19 |
| 2 | 18 | 3 | Ϻ┘äϡϻϺϻ | 2017-01-24 19:13:36 |
| 3 | 3 | 3 | Ϻ┘äÏÀÏ¿┘èÏ¿┘è | 2017-01-24 19:15:48 |
+----+------------+---------+----------------+---------------------+
mysql> select * from en_txt limit 3;
+----+---------+------------------+
| id | file_id | text |
+----+---------+------------------+
| 1 | 2 | Apothecary Cheng |
| 2 | 2 | Blacksmith Ho Li |
| 3 | 2 | Apothecary Sung |
+----+---------+------------------+
mysql> select * from trans_files limit 3;
+----+-----------+-----------+
| id | game_name | file_name |
+----+-----------+-----------+
| 1 | drb | skills |
| 2 | drb | npcs |
| 3 | drb | test |
+----+-----------+-----------+
in this case i am using PDO and query to execute it
PHP code
so why does it take a long time?

sort data when loop by a calculated column

someone can help me to sort this table by calculated $changepercentage please.
My table has more than 1000 records and it's uploaded by date order . I tried arsort() , SORT() when it's echo but PHP says it's not an array but a string.
I have tried mysqli_fetch_array() also but no luck.
need your kind help.
clo_prices (table)
date | name | cl_price |
2015-09-15 | AAA | 10 |
2015-09-15 | BAA | 20 |
2015-09-15 | ABA | 30 |
2015-09-16 | AAA | 86 |
2015-09-16 | BAA | 65 |
2015-09-16 | ABA | 33 |
price_list (table)
date | name | volume | last |
2015-09-15 | AAA | 6655245 | 56 |
2015-09-15 | ABA | 5666 | 65 |
2015-09-15 | AAB | 1333 | 33 |
2015-09-16 | AAA | 112365 | 56 |
2015-09-16 | ABA | 23131 | 8 |
2015-09-16 | AAB | 23330 | 12 |
2015-09-16 | ABB | 2323122 | 60 |
2015-09-16 | ACB | 21222 | 99 |
Desired Result
date | company | closing | volume | Change % |
2015-09-16 | AAA | 56 | 6655245 | 66 |
2015-09-16 | ABA | 65 | 5666 | 9.7 |
2015-09-16 | ACB | 69 | 1333 | 7 |
2015-09-16 | AAB | 33 | 112365 | 3.2 |
$queryTMG = "SELECT price_list.date, price_list.name, price_list.last, price_list.volume,
clo_prices.date, clo_prices.name, clo_prices.cl_price
FROM price_list
INNER JOIN clo_prices
ON price_list.name = clo_prices.name
WHERE price_list.date= '$dateT' AND
clo_prices.date = '$dateT'
ORDER BY cl_price DESC LIMIT 10 ";
$queryPMG = "SELECT price_list.date, price_list.name, price_list.last, price_list.volume,
clo_prices.date, clo_prices.name, clo_prices.cl_price
FROM price_list
INNER JOIN clo_prices
ON price_list.name = clo_prices.name
WHERE price_list.date= '$dateT' AND
clo_prices.date = '$dateP'
ORDER BY cl_price DESC LIMIT 10";
$resultTMG = mysqli_query($dbc, $queryTMG) or die (mysqli_error());
$resultPMG = mysqli_query($dbc, $queryPMG) or die (mysqli_error());
echo '<table cellpadding = 15 border =1>';
echo '<tr>
<th>Company</th>
<th>Closing Rs.</th>
<th>Volume</th>
<th>Change %</th>
</tr>';
while($rowTMG = mysqli_fetch_assoc($resultTMG) AND $rowPMG = mysqli_fetch_assoc($resultPMG))
{
echo '<tr> <td>'. $rowTMG['name']. '</td>';
echo '<td>'. $rowTMG['last']. '</td>';
echo '<td align = "right">'. $volumeTMG = number_format($rowTMG['volume']). '</td>';
echo '<td align = "right">'.$changeprecentage =
number_format((( $rowTMG['cl_price'] - $rowPMG['cl_price'] ) /
$rowTMG['cl_price']), 2, '.', '' ). '</td> </tr>';
}
echo '</table>';
I probably have not quite understood yet how you compute your percentages, because using the data given I get different results, see my fiddle here
SELECT p.date, p.name, p.last, p.volume,
d.cl_price+0 prevprice,
c.cl_price+0 currprice,
100*(c.cl_price-d.cl_price)/c.cl_price perc_change
FROM price_list p
INNER JOIN clo_prices c ON p.name = c.name -- current prices
AND p.date = c.date
INNER JOIN clo_prices d ON p.name = d.name -- previous prices
AND '2015-09-15' = d.date
WHERE p.date= '2015-09-16'
ORDER BY perc_change desc
This solution is so far only a MySQL select statement, since everything can be done in a single query. You will probably have to sort out some of the logic or the input data.
For the purpose of the example I assumed that $dateT is '2015-09-16' and $dateP is '2015-09-15'.

Codeigniter join multiple tables with where clauses

I have four tables that I need to join together, with one of those tables having two different records I need to grab. The guild and party tables I'm getting the name of those so that I can display the name instead of ID to the user, and the charlog table has the char_id's creation date and last login date that I want to show as well. Here are the tables:
char table:
----------------------------------------------------
| char_id | name | guild_id | party_id |
|---------------------------------------------------
| 150000 | char1 | 3 | 3 |
| 150001 | char2 | 2 | (NULL) |
| 150002 | char3 | (NULL) | 1 |
| 150003 | char4 | 1 | 2 |
----------------------------------------------------
guild table:
-------------------------
| guild_id | name |
-------------------------
| 1 | guild_1 |
| 2 | guild_2 |
| 3 | guild_3 |
-------------------------
party table:
-------------------------
| party_id | name |
-------------------------
| 1 | party_1 |
| 2 | party_2 |
| 3 | party_3 |
-------------------------
charlog table:
----------------------------------------------------
| time | char_id | char_msg |
----------------------------------------------------
| 2015-02-14 06:45:32 | 150000 | make new char |
| 2015-02-14 06:45:58 | 150000 | char select |
| 2015-02-15 12:32:19 | 150001 | make new char |
| 2015-02-15 16:54:01 | 150000 | char select |
| 2015-02-15 19:23:54 | 150001 | char select |
| 2015-02-16 01:32:13 | 150002 | make new char |
| 2015-02-16 01:33:01 | 150003 | make new char |
| 2015-02-16 04:45:43 | 150000 | char select |
| 2015-02-16 07:43:22 | 150003 | char select |
----------------------------------------------------
As mentioned, I need to get the make new char entry from the charlog table to display when the character was created and as well the LAST (by date) char select entry to display when the character was played, all of this with one single char_id.
All in all, I'd be looking for a table that looks like this:
---------------------------------------------------------------------------------------------------------------------
| char_id | name | guild_id | guild_name | party_id | party_name | create_time | lastlogin_time |
---------------------------------------------------------------------------------------------------------------------
| 150000 | char1 | 3 | guild_3 | 3 | party_3 | 2015-02-14 06:45:32 | 2015-02-16 04:45:43 |
I'm using the following active record lines in Codeigniter to attempt to get the data I need. It returns the create_date correctly but the lastlogin_time is not returned (it's blank):
function get_char_info($cid) {
$this->db->select('char.*,guild.guild_id,guild.name AS guild_name,party.party_id,party.name AS party_name,charlog1.time AS create_time,charlog2.time AS lastlogin_time');
$this->db->from('char');
$this->db->where('char.char_id', $cid);
$this->db->join('guild', 'char.guild_id = guild.guild_id', 'left');
$this->db->join('party', 'char.party_id = party.party_id', 'left');
$this->db->join('charlog AS charlog1', 'char.char_id = charlog1.char_id AND charlog1.char_msg = "make new char"', 'left');
$this->db->join('charlog AS charlog2', 'char.char_id = charlog2.char_id AND charlog2.char_msg = (SELECT max(charlog.time) FROM charlog WHERE char_msg = "char select")', 'left');
$query = $this->db->get();
return $query->row();
}
As mentioned, the guild_name, party_name and create_time come through correctly, but the lastlogin_time is blank, no error.
I've tried jumbling some things around in the active record clauses but can't get the lastlogin_time to show. Any help would be appreciated.
The join on your subquery is not correct :
It should be charlog2.time = (SELECT....) not charlog2.char_msg = (SELECT....)

PHP select lowest ID of Group

How can I call the lowest ID (nNr) and the entry next to it ($cPfad) in PHP? I have 2 Tables and try to get from both of them the Details of an ID.
My SQL
SELECT
p1.kArtikel,
p1.cName,
p1.cKurzBeschreibung,
p1.dLetzteAktualisierung,
p1.dErstellt,
p1.cSeo,
p2.kartikelpict,
p2.nNr,
p2.cPfad
FROM tartikel AS p1
JOIN tartikelpict AS p2
ON (p1.kArtikel = p2.kartikelpict)
JOIN (SELECT kartikelpict, MIN(nNr) min_nNr FROM tartikelpict GROUP BY kartikelpict) p3
ON (p2.kartikelpict = p3.kartikelpict AND p2.nNr = p3.min_nNr)
ORDER BY p1.kArtikel
DESC LIMIT 10
DB Tables
+----------+-------+---------------------+------------+-------+
| kArtikel | cName | cKurzBeschreibung | dErstellt | cSeo |
+----------+-------+---------------------+------------+-------+
| 560 | Title | Short Description | 2014-03-25 | title |
+----------+-------+---------------------+------------+-------+
+--------------+--------------+-------+
| kartikelpict | cPfad | nNr |
+--------------+--------------+-------+
| 560 | picture4.jpg | 4 |
| 560 | picture3.jpg | 3 |
| 560 | picture2.jpg | 2 |
| 560 | picture.jpg | 1 |
+--------------+--------------+-------+
My PHP Code
while ($row = mysql_fetch_assoc($result)){
$cName = $row['cName'];
$cKurzBeschreibung = $row['cKurzBeschreibung'];
$dLetzteAktualisierung = $row['dLetzteAktualisierung'];
$dErstellt = $row['dErstellt'];
$cSeo = $row['cSeo'];
$date = strtotime($row['dErstellt']);
$pubdate = date(r, $date);
$id = $row['kArtikel'];
$nNr = $row['p3.min_nNr'];
$cPfad = $row['cPfad'];
echo"<item>";
echo"<title><?php echo $cName; ?></title>";
echo"<link>http://domain.com/".$cSeo."</link>";
echo"<guid isPermaLink='false'>http://domain.com/".$cSeo."></guid>";
echo"<pubDate>".$date."</pubDate>";
echo"<description><![CDATA[ <img src='http://domain.com/bilder/produkte/normal/".$row['nNr']."' />".$cKurzBeschreibung." ]]></description>";
echo"<enclosure url='http://domain.com/bilder/produkte/normal/'".$nNr." type='image/jpeg' />";
echo" </item>";
}
The call of $cPfad does not output anything, the call of of $nNr call each ID instead of the lowest one to get the correct $cPfad. What am I doing wrong im my PHP Code?

Categories