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?
Related
This question already has answers here:
How to join two tables mysql?
(4 answers)
Closed 2 years ago.
I have two tables that looks something like this (made as example):
Table sales:
| ID | date | displayname | status |
| 1 | 2020/08/03 16:25:26 | Angel | OK |
| 2 | 2020/08/03 16:25:26 | Angel | OK |
| 3 | 2020/08/03 16:25:26 | Cabil | X |
| 4 | 2020/08/03 16:25:26 | Syed | OK |
...
Table users (all of the columns has value, but removed for GDPR reasons):
| ID | displayname | fullname | email |
| 1 | Angel | | |
| 2 | Nico | | |
| 3 | Raquie | | |
| 4 | Cabil | | |
| 5 | Syed | | |
...
I have a PHP script that looks like this:
<?php
$query = "SELECT * FROM sales WHERE status='OK' ORDER BY STR_TO_DATE(`date`, '%Y/%m/%d %H:%i:%s') DESC LIMIT 5";
if ($result = $link->query($query)) {
$num_rows = 0;
while ($row = $result->fetch_assoc()) {
$num_rows++;
echo '<div class="my-box">';
echo "{$row['id']}";
echo "{$row['date']}";
echo "{$row['dbirth']}";
echo "{$row['email']}";
echo "{$row['displayname']}";
echo '</div>';
}
$result->free();
}
?>
Now it currently displays each displayname for each sale in echo "{$row['displayname']}";, but insted of the displayname, I want to show the fullname for the user that has the current display name. How can I accomplish this?
You seem to be looking for a join:
select s.*, u.fullname
from sales s
inner join users u on u.displayname = u.displayname
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.
Am trying to group user orders by the order id, but am not getting it right i know this will be first get done in the SQL query then organise it well in PHP and HTML but i don't know how to get it done.
orderinfo
oid | userid | total | payid
-----|---------|--------|----------
oi10 | peter | 650 | VCC-100
oi12 | john | 30 | VCC-500
oi15 | peter | 60 | COD-500
itemorder
pid | ioid | userid | price | qty | itemname
----| ------|---------|--------|-----|-----------
p10 | oi10 | peter | 200 | 1 | lexus
p20 | oi10 | peter | 150 | 1 | Toyota
p15 | oi10 | peter | 300 | 1 | Myvi
p66 | oi15 | peter | 25 | 2 | BMW
p67 | oi15 | peter | 10 | 1 | Saga
p67 | oi12 | john | 10 | 3 | Saga
My current Code
$handler->prepare('
SELECT * FROM itemorder io
LEFT JOIN orderinfo oi
ON io.oid = oi.ioid
WHERE io.userid = 'peter'
GROUP BY io.oid
ORDER BY oi.pid DESC
');
$handler->execute();
$RecentOrder = $handler->getAll();
$handler->free();
if(!empty($RecentOrder)){
foreach($RecentOrder as $row){
}
}
Expected Result
I want the result to be sorted according to the order id all item that has same order id will be listed (list according to order id).
oid: oi10
--------
lexus
Toyota
Myvi
---------------------
oid: oi15
--------
BMW
Saga
The desired output can be retrieved with just ORDER BY.
SELECT *
...
ORDER BY oi.oid DESC, io.pid DESC
And then do the specific formatting in PHP. Easiest way is probably to remember the order_id of the last row.
$lastOrderId = null;
foreach ($result as $row) {
if ($lastOrderId != $row['oid']) {
echo 'oid: ' . $row['oid'] . PHP_EOL;
echo ' -----------' . PHP_EOL;
}
echo ' ' . $row['itemname'] . PHP_EOL;
$lastOrderId = $row['oid'];
}
You can try this :
SELECT io.ioid,GROUP_CONCAT("",io.itemname) as order_items FROM itemorder as io
LEFT JOIN orderinfo as oi
ON io.ioid = oi.oid
WHERE io.userid = 'peter'
GROUP BY io.ioid
ORDER BY io.pid DESC
Please not the columns on which join is done.
Then in PHP you can use explode function to get the array of names for each order.
Hope this helps!
I have 3 table now:
First is : member_username
+-------------+------------------+
| uid | username |
+-------------+------------------+
| 1 | theone |
| 2 | ohno |
| 3 | prayforpr |
+-------------+------------------+
Second is : member_data
+-------------+-------------------+-----------------+
| uid | talk | etc |
+-------------+-------------------+-----------------+
| 1 | talk1 | |
| 2 | talkeee | |
| 3 | iojdfnl | |
+---------------------------------------------------+
Third is : member_level
+-------------+-------------------+-----------------+
| uid | level | fid |
+-------------+-------------------+-----------------+
| 1 | 2 | 1 |
| 1 | 10 | 2 |
| 2 | 1 | 1 |
| 2 | 99 | 2 |
| 1 | 40 | 3 |
| 3 | 50 | 1 |
| 1 | 44 | 4 |
+---------------------------------------------------+
I would like to query data and display the only one uid when member_level is higher in when SUM member_level.level Where fid in 1,2,3.
my query now is like below, but this query is sum all the level including fid 4 also, how to specify only sum in fid 1,2,3? and how do I assign the SUM of member_level.level Where fid in 1,2,3 to $levelKingTotalLevel?
$levelKing = DB::query("SELECT t1.uid,t1.username,t2.talk FROM ".DB::table('member_level')." t3 JOIN ".DB::table('member_username')." t1 ON(t3.uid = t1.uid) JOIN ".DB::table('member_data')." t2 ON (t1.uid = t2.uid) GROUP BY t3.uid ORDER BY SUM(t3.level) DESC LIMIT 1");
while($rowlevelKing = DB::fetch($levelKing)) {
$levelKingTotalLevel = $rowlevelKing['???'];
$levelKingN = $rowlevelKing['username'];
$levelKingUID = $rowlevelKing['uid'];
$levelKingT = $rowlevelKing['talk'];
};
echo "The ".$levelKingN." total level is ".$levelKingTotalLevel." and he talk about ".$levelKingT;
Thank you.
To filter records having fid values as 1, 2 or 3, use IN statement in WHERE clause. Alias totalLevel in select statement will give you total level for a user.
SELECT t1.uid, t1.username, t2.talk, SUM(t3.level) AS totalLevel
FROM member_level t3
JOIN member_username t1
ON (t3.uid = t1.uid)
JOIN member_data t2
ON (t1.uid = t2.uid)
WHERE t3.fid IN (1,2,3)
GROUP BY t3.uid
ORDER BY totalLevel DESC
LIMIT 1
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?