Mysql count with descending with join and where clause ,two tables, limit? - php

Here i have implemented two tables (pages, pagestatistics).I need result for last five records using join from the those tables and also i need a count of cID field from second table (pagestatistics), i need that count result will be display in descending.
NOTE : Primary id is cID.
![pagestatistics][1]![pages][2]
<?php
$recentpageviews =mysql_query("SELECT Distinct(cID) FROM `pagestatistics` order by `pstID` desc limit 0,5");
$downloads=mysql_num_rows($recentpageviews);
$k=1;
$cid="";
while($views_values=mysql_fetch_array($recentpageviews))
{
$cid.=",".$views_values['cID'];
$k++;}
}
$explode =explode(",",$cid);
for($i=1;$i<count($explode);$i++)
{
$sql=mysql_query("select Distinct(a.cID),count(a.cID) as clicks,b.cFilename,a.date from pagestatistics as a left join pages as b on a.cID=b.cID where a.cID='".$explode[$i]."' order by a.cID desc");
$res=mysql_fetch_array($sql);
?>
<tr>
<td class='ccm-site-statistics-downloads-title'><?php echo $res['cFilename'];?></td>
<td class='ccm-site-statistics-downloads-title'><?php echo $res['clicks'];?></td>
<td class='ccm-site-statistics-downloads-title'><?php echo $res['date'];?></td>
</tr>
<?php }?>
How to display all records in descending order.
Thank in advance.

You can do it in this way just order by with your count separated with , in ORDER BY clause
$sql=mysql_query("select Distinct(a.cID),count(a.cID) as clicks,b.cFilename,a.date
from pagestatistics as a left join pages as b on a.cID=b.cID
where a.cID='".$explode[$i]."' order by a.cID,clicks desc");

Related

Join Two MySQL Table and Display Result in Table

I have two table called t_user and t_chat. I am trying to display message from t_chat in PHP table like below.
$quotes_qry="SELECT * FROM t_chat ORDER BY id DESC LIMIT $start, $limit";
$result=mysqli_query($mysqli,$quotes_qry);
<?php
$i=0;
while($row=mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo ++$sr+$start;?></td>
<td><?php echo $row['sender'];?></td>
<td><?php echo nl2br($row['receiver']);?></td>
<td><?php echo nl2br($row['message']);?></td>
<td><?php echo time_elapsed_string($row['time']);?></td>
<td><img src="images/delete-icon.png"></td>
</tr>
<?php
$i++;
}
?>
I want display sender and receiver name which is located in table called t_user with username column. I am new in PHP and confused how can I achieve it. Let me know if someone can help me for achieve my task. Thanks a lot!
Note : t_chat table have userid witch column name called sender and receiver, currently I am displaying that userid in above table, instead I want display username.
Thanks
The joins might look something like this:
SELECT t_chat.sender AS 'sender', t_chat.receiver AS 'receiver', t_chat.message AS 'message', t_chat.time AS 'time', t_chat.id AS 'id', user1.username AS 'senderusername', user2.username AS 'receiverusername'
FROM t_chat
LEFT JOIN t_user AS user1 ON t_chat.sender = user1.id
LEFT JOIN t_user AS user2 ON t_chat.receiver = user2.id
ORDER BY id DESC
In this example I am joining the tables twice (as user1 and user2) so that the t_user table gets referenced independently for each lookup.
I also gave each column a name using AS to make them easier to reference later in your code.
Try this sql (The t_user table must have an id column that matches the userid from t_chat):
$quotes_qry="SELECT t1.sender, t1.receiver, t1.message, t1.time, t2.username FROM t_chat AS t1 INNER JOIN t_user AS t2 ON t1.userid=t2.id ORDER BY t1.id DESC LIMIT $start, $limit";
More details and examples about MySQL JOIN you can find in the tutorial from:
https://coursesweb.net/php-mysql/mysql-inner-left-join-right-join_t

select top 5 sellers in mysql

I have the follow table in MySQL
TABLE "SALES"
id, product, code, quantity, amount, who-sold-it
while the field "who-sold-it" is just a example to understand the question but not the real name of the field
Now I have another table the name is "USERS" and it looks like this
id, name, sellercode
I need to get the top 5 sellers using the 2 tables looking the who-sold-it in each SALE and display their name and sold amounts
Order the results by total sales and take the top 5 with limit. You can also join the sales table with the seller table to get the name of the seller.
select users.name, users.sellercode, sum(sales.amount) as total
from sales, users
where sales.sellercode = users.sellercode
group by users.sellercode, users.name
order by total desc
limit 5
To display the result:
<?php while ($row = mysqli_fetch_assoc($result)) ?>
<tr>
<td><?php echo htmlspecialchars($row['name'])</td>
<td><?php echo htmlspecialchars($row['total'])</td>
</tr>
<?php } ?>

My query mixes up with arrays on execution

Right, hello.
I've been working on a website template and I've coded a forum-like section, but simply discussion threads, and now, I'm having struggles with a specific query. This.
SELECT * FROM cms_discussions d JOIN cms_discussions_comments c ON d.id = c.discussionid ORDER BY c.time DESC LIMIT 10
I thought it would be easy enough to see how others have their question answered on how to order one table by another table's row. So I tried their method, but now it's mixing it up with each other. This is how it used to look like when I was sorting the threads by their IDs
SELECT * FROM cms_discussions ORDER BY id DESC LIMIT 10
And this is how it looked.
While using the SQL query I first provided, the outcome came out kinda wrong. The author of the thread was changed to 'server' (which was the author field from cms_discussions_comments), so basically, the 'author' field from cms_discussion gets replaced with the 'author' field from cms_discussions_comments. How I display the threads are quite simply.
<?php
$response = $databaseHandle->query("SELECT * FROM cms_discussions d JOIN cms_discussions_comments c ON d.id = c.discussionid ORDER BY c.time DESC LIMIT 10");
while($row = $response->fetch_assoc()) {
stripslashes_array($row);
?>
<table class="row" onclick="window.location.href = '/discussion/<?php echo $row["id"]; ?>/';">
<tr>
<td style="width: 50px"><img src="/styles/<?php echo $styleConfig["theme"]; ?>/album/discussions/<?php echo ($row["active"] == 1)?("status_info"):("status_locked"); ?>.png"></td>
<td><p id="title"><?php echo $row["title"]; ?></p><br><p id="description">Posted <?php echo date("F j, Y, g:i A", $row["posted"]); ?> by <i><?php echo $row["author"]; ?></i>.</p></td>
</tr>
</table>
<?php } ?>
<?php } ?>
I'm really not sure what the issue is here since I usually just do basic queries. Thanks in advance for any assistance.
Edit: Forgot to provide the second screenshot:
Since your two tables cms_discussions and cms_discussions_comments both contain an author field, one of the twos is not going to be accessible in your result array if you just do SELECT *.
What you need to do is to explicitly select fields and assign aliases so that there is no overlaps.
You could do something like:
SELECT d.*, c.author AS comment_author FROM cms_discussions d JOIN cms_discussions_comments c ON d.id = c.discussionid ORDER BY c.time DESC LIMIT 10
With that query, your $row['author'] would refer to the field of cms_discussions while $row['comment_author'] would refer to the field of cms_comments. That being said, you might want to do the same for other fields like id.
If you simply do not want to select columns from cms_discussions_comments then this will do the trick:
SELECT d.* FROM cms_discussions d JOIN cms_discussions_comments c ON d.id = c.discussionid ORDER BY c.time DESC LIMIT 10

Converting two loops to smarty

I have two cycles in PHP, that I needed converting to smarty structure. Down includes PHP code.
Code:
<pre>
$query = mysqli_query($cnn, "SELECT *, COUNT(*) AS ph FROM course INNER JOIN completed_course ON course.id = completed_course.id_course GROUP BY course.id");
while ($row = mysqli_fetch_array($query)){
<tr> <td> <?php echo $row['id']; ?></td><td> <?php echo $row['nazev']; ? </td><td>
?php echo $row['ph']; ? </td> <td>
?php
$Number_of_graduates = mysqli_query($cnn, "SELECT COUNT(*) AS abs FROM participant where id_completed_course = $row[id]");
while ($rAbs = mysqli_fetch_array($Number_of_graduates)){
echo $rAbs['abs'];
} ?
</td>
</pre>
The question is. How to convert the second loop where first id from SQL?
Ok, so your question is really about SQL. Let's look at your queries. The first one looks like this:
SELECT *, COUNT(*) AS ph
FROM course
INNER JOIN completed_course ON course.id = completed_course.id_kurz
GROUP BY course.id
I'm assuming (because I don't know anything about your database scheme) that this is going to give a list of courses and the number of students (maybe - I don't know what's in the completed_course table) who completed them. As written, this query is going to also give you some data from the completed_course table, but it's likely to be meaningless since you're grouping only on course.id.
The second query is:
SELECT COUNT(*) AS abs
FROM participant
WHERE id_completed_course = {DATA FROM FIRST QUERY}
Presumably, this query is intended to give you the total number of participants of completed courses. To make that work, the WHERE clause could look like this:
SELECT COUNT(*) AS abs
FROM participant
WHERE id_completed_course IN (
SELECT course.id FROM
FROM course
INNER JOIN completed_course ON course.id = completed_course.id_kurz
)
Notice that I'm taking the values selected in the first query and making them part of an IN clause. This query can be simplified significantly - for instance, the JOIN in the subquery is really only selected ID values from the completed_course table:
SELECT COUNT(*) AS abs
FROM participant
WHERE id_completed_course IN (
SELECT id_kurz
FROM completed_course
)
And the query will actually be more efficient if you get rid of the subquery altogether and just join the participants to the completed_course table.
SELECT COUNT(*) AS abs
FROM participant
INNER JOIN completed_course ON participant.id_completed_course=completed_course.id_kurz
This last query is going to give you one value: the number of participants whose id_completed_course value corresponds to an item in the completed_course table. You can use the mysqli methods to retrieve this data and pass it to your Smarty template.

SUM of a values in a column in MySQL and group them [duplicate]

This question already has answers here:
Sum values in a MySql column [closed]
(2 answers)
Closed 9 years ago.
I need to design a leader board for a quiz website that I am making. I store the user scores in a database table called 'user_record' which has the following structure:
'user_id' - varchar(254) -which holds the user id for a particular score
'score' - int(2) -which holds the score
'time' - timesatamp -which has the CURRENT_TIMESTAMP
'date' - date -which has the CURDATE()
Now I need to sum the values for each user_id for the current date and display the top 5 in a descending order.
I have the following code for this, however it doesn't seem to be working. Any help?
<table>
<tr>
<th>User Id</th>
<th>Score</th>
</tr>
<?php
include "connection.php";
$w=mysql_query("SELECT user_id,SUM(Score) from score WHERE date=CURDATE() GROUP BY user_id ORDER BY score DESC limit 5");
$b=$row=mysql_fetch_array($w);
while($b)
{
$user_id=$row['user_id'];
$score=$row['score'];
?>
<tr>
<td><?php echo $user_id;?></td>
<td><?php echo $score; ?></td>
</tr>
<?php
}
?>
</table>
replace this
$w=mysql_query("SELECT user_id,SUM(Score) from score WHERE date=CURDATE() GROUP BY user_id ORDER BY score DESC limit 5");
$b=$row=mysql_fetch_array($w);
while($b)
by
$w=mysql_query("SELECT user_id,SUM(Score) as score from user_record WHERE date=CURDATE() GROUP BY user_id ORDER BY score DESC limit 5");
while( $row=mysql_fetch_array($w) )
the right query is
SELECT user_id, SUM(score) from user_record WHERE date=CURDATE() GROUP BY user_id ORDER BY SUM(score) DESC limit 5

Categories