this is how I should use distinct to download the content again.
Here at pictures VISR it to the download content 3 times, but I only want it 1 time in total
what is the problem is that it does not pick all of them, but I only want it to pick up only one of time, which means that it only need to download one time by title and simultaneously username.
$sql = "SELECT DISTINCT fms_bruger.fornavn, fms_bruger.efternavn, fms_opslagpm.id, fms_opslagpm.title FROM fms_bruger INNER JOIN fms_opslagpm ON fms_bruger.id=fms_opslagpm.fra_id WHERE fms_opslagpm.til_id = ? ORDER BY fms_opslagpm.datotid DESC";
if ($stmt = $this->mysqli->prepare($sql)) {
$stmt->bind_param('i', $id);
$id = $_SESSION["id"];
$stmt->execute();
$stmt->bind_result($fornavn, $efternavn, $id, $title);
while ($stmt->fetch()) {
?>
<tr class="postbox">
<td class="beskedinfo">
<p><?php echo $fornavn . " " . $efternavn;?></p>
</td>
<td>
<?php echo $title;?>
</td>
<td>
Slet
</td>
</tr>
<?php
}
$stmt->close();
}
You need to use GROUP BY not DISTINCT to get the grouping of the title field.
SELECT fms_bruger.fornavn, fms_bruger.efternavn, fms_opslagpm.id, fms_opslagpm.title
FROM fms_bruger
INNER JOIN fms_opslagpm ON fms_bruger.id=fms_opslagpm.fra_id
WHERE fms_opslagpm.til_id = ?
GROUP BY fms_opslagpm.title
ORDER BY fms_opslagpm.datotid DESC
SELECT DISTINCT does exactly what it says: it selects distinct rows.
You include id in the columns. I'm pretty sure IDs will be unique, therefore all rows are DISTINCT.
Consider removing the DISTINCT keyword and adding a GROUP BY `fms_bruger`.`title` clause. This is an extension to the SQL standard which will return an arbitrary row for each unique title.
Related
I need to know how to use MAX() and COUNT() query to display the "fiser" table entries that contain the primary "codp" key that comes from the "products" table, depending on a previously selected period?
Table products : codp, denp ;
Table orders: codc,codp ;
Table returns : codr, datar, codc, codp
<?php
if(isset($_POST['add'])){
$sql = "SELECT p.denp, a.datar,MAX(p.codp)
FROM ( SELECT COUNT(p.codp) FROM products p ) products p
INNER JOIN orders o ON p.codp=o.codp
INNER JOIN returns r ON o.codc=r.codc
WHERE r.datar>=STR_TO_DATE('".$_POST['d1']."','%Y-%m-%d')
AND r.datar<=STR_TO_DATE('".$_POST['d2']."','%Y-%m-%d') ";
$result = mysqli_query($conn, $sql);
$queryResult = mysqli_num_rows($result);
if($queryResult > 0 ){
while ($row = mysqli_fetch_assoc($result)) {
echo "
<table border=1 >
<tr>
<td><p> ".$row['codp']." </p></td>
<td><p> ".$row['denp']." </p></td>
<td><p> " .$row['codr']." </p></td>
<td><p> " .$row['datar']." </p></td>
<td><p> " .$row['codc']." </p></td>
</tr> </table> ";
}
} else {
echo " No results!" ;
}
}
?>
You should use group by for codp and the join the related result eg:
$sql = "SELECT p.denp, r.datar,MAX(p.cnt_codp)
FROM (
SELECT codp, COUNT(*) as cnt_codp FROM products
group by codp
) products p
INNER JOIN orders o ON p.codp=o.codp
INNER JOIN returns r ON o.codc=r.codc
WHERE r.datar>=STR_TO_DATE('".$_POST['d1']."','%Y-%m-%d')
AND r.datar<=STR_TO_DATE('".$_POST['d2']."','%Y-%m-%d')
GROUP BY p.denp, r.datar ";
and you should check for your db driver for param bindig instead of use of php var in sql (you are at risk for sql injection)
and you should also use a group by for not aggreated column or reeval the question if you need the values related to max result (the use of aggregation function without a proper group by columns for not aggreagated is depreacted in sql ai the most recent version of mysql is not allowed)
My goal is to display all user's names and their availability for a specific match. The following code does that. Might not be the easiest way but I am new to this and it works for now.
//Display match dates and times(showing all matches)
$stmt = $conn->prepare('SELECT * FROM matches
WHERE tid=:tid /*AND datetime BETWEEN (NOW() + INTERVAL 24 HOUR) AND (NOW() + INTERVAL 28 DAY)*/
ORDER BY datetime');
$stmt->bindParam(':tid', $tid, PDO::PARAM_INT);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo "
<h2>".date("F j",strtotime($row["datetime"])). " # ".date("g:i A",strtotime($row["datetime"]))."<br></h2>
<h3>"$row[location] Match <br> vs $row[opponent]</h3>";
echo "<table>
<tr>
<th>Player</th>
<th>Availability</th>
</tr>
</table>";
//Get available(Yes) users first and last names to display
$stmtY = $conn->prepare("SELECT user.first, user.last
FROM user
INNER JOIN match_part ON user.uid=match_part.uid
WHERE mid=:mid AND availability = 'Yes'");
$stmtY->bindParam(':mid', $row['mid'], PDO::PARAM_INT);
$stmtY->execute();
while ($rowY = $stmtY->fetch(PDO::FETCH_ASSOC))
{
echo "<table class='match-avail'>
<tr>
<td>$rowY[first] $rowY[last]</td>
<td><span style='color: #70db62'>✔</span></td>
</tr>
</table>";
}
//Get unavailable(No) users first and last names to display
$stmtN = $conn->prepare("SELECT user.first, user.last
FROM user
INNER JOIN match_part ON user.uid=match_part.uid
WHERE mid=:mid AND availability = 'No'");
$stmtN->bindParam(':mid', $row['mid'], PDO::PARAM_INT);
$stmtN->execute();
while ($rowN = $stmtN->fetch(PDO::FETCH_ASSOC))
{
echo "<table class='match-avail'>
<tr>
<td class='Avail_No'>$rowN[first] $rowN[last]</td>
<td><span style='color: #b20000'>✘</span></td>
</tr>
</table>";
}
The above gives me the exact information I want.
Where I am stuck is...
I have team_members table (tmid, uid, tid) with a list of all players on a given team
A match_part table (mpid, mid, uid, availability) with the availability of the players who have entered their availability for a specific match.
I now want to show the people who have not yet given their availability. Therefore, I need to show the team_members names who do not exist in the match_part table. So I need to display only the names of players that are in the team_members table and not in the match_part table.
So something along the lines of...
$stmtU = $conn->prepare("SELECT user.first, user.last
FROM user
INNER JOIN team_members ON user.uid=team_members.uid
INNER JOIN match_part ON user.uid=match_part.uid
WHERE ***team_member.uid is not in match_part.uid***");
$stmtU->bindParam(':tid', $tid, PDO::PARAM_INT);
$stmtN->bindParam(':mid', $row['mid'], PDO::PARAM_INT);
$stmtU->execute();
while($rowU = $stmtU->fetch(PDO::FETCH_ASSOC))
{
echo "<table class='match-avail'>
<tr>
<td>$rowU[first] $rowU[last]</td>
<td>?</td>
</tr>
</table>";
}
Sorry for the lengthy post but I figured the information might help understanding the goal here. Thank you to all who have any thoughts/solutions. Your help is greatly appreciated!
if i understand correct you need the team_members.uid that are not in match part
for this you could use a not in with subselect
$stmtU = $conn->prepare("SELECT user.first, user.last
FROM user
INNER JOIN team_members ON user.uid=team_members.uid
where team_members.uid not in ( select uid from match_part)");
As shown in the picture below, there are redundant records. How can the redundancy be removed?
Here's my code:
<?php
$YearNow=Date('Y');
include('../connection/connect.php');
$result = $db->prepare("SELECT * FROM studentvotes,student where student.idno = studentvotes.idno");
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>
<tr class="record" style="text-align:center;">
<td><?php echo $row['idno']; ?></td>
<td><?php echo $row['candid']; ?></td>
</tr>
<?php
}
?>
You need to use GROUP BY.
Change your SQL To:
SELECT * FROM studentvotes,student where student.idno = studentvotes.idno
GROUP BY student.idno
With GROUP BY, you specify a column (even multiple columns) to group with.
The results are grouped by those columns it means those columns (combinations if multiple) will come only once in the result set.
For example, your table has multiple entries of iDno.
When we fire SELECT query, it will return all rows having multiple instances of iDno.
If we apply GROUP BY, it will return results grouped by iDno.
just add groupby to your query like
$result = $db->prepare("SELECT * FROM
studentvotes,student
where student.idno = studentvotes.idno
GROUP BY student.idno
");
for more details please read #pupil Answer.
i hope this is working for you.
You can use GROUP BY for not displaying redundant records.
$result = $db->prepare("SELECT *
FROM studentvotes,
student
WHERE student.idno = studentvotes.idno
GROUP BY student.idno
");
I have one table in database -> applicants_detail which has two column
applied_personid and applied_courseid.
College provides number of courses.
I have the given id to applied_courseid, that is written like this:
arts--1
science--2
maths--3...
And applied_personid which contains applied person id.
I need to count how many people applied for the course, like in the page it should show:
maths------15 people_applied
science------20 people_applied
I tried this query:
$query="select * from people where people_id in(select applied_personid from applicants where applied_courseid =".$_GET['postcourseid']." )";
And the code to find count is not able to show count in the table.
<?php
$count=0;
while($row=mysql_fetch_array($res))
{
echo '<tr> <td width="10%">'.$count.'
<td width="50%">'.$row['student_fnm'].'
<td width="50%">'.$row['applied_courseid'].'
<td width="30%">course name
';
$count++;
}
?>
You just need to group by applied_courseid. This should do it.
select applied_courseid, count(applied_personid) as `count`
from
applicants_detail
group by applied_courseid
Then if needed use the results here to join to the other tables
which you probably have (that would give the you course name e.g.).
Try this to get all data:
SELECT course_name, applied_courseid as course_id, count(applied_personid) as `student_number`
FROM applicants_detail
INNER JOIN course_detail ON course_detail.course_id = applicants_detail.course_id
GROUP BY applied_courseid
I have 2 table of date , student_info and student_payment in my databace...
in student_info i have:
id, student_id,student_mail,student_pass,student_name,...
and in student_payment have:
id,student_id,student_payment_id,student_payment_date,...
so my problem is here, i wanna select student_name where student_id form student_info but i have problem and mysql give my an error:
$db->connect();
$sql = "SELECT * FROM `student_payment`";
$rows = $db->fetch_all_array($sql);
$student_id = $rows['student_id'];
$sql2 = "SELECT * FROM `student_info` WHERE student_id=$student_id";
$rows2 = $db->fetch_all_array($sql2);
$db->close();
foreach($rows as $record ){
// i wanna to use student_name in first line
echo "\n<tr>
<td>$record[student_id]</td>
<td dir=\"ltr\">$record[student_payment]</td>
<td dir=\"ltr\">$record[student_payment_id]</td>
<td dir=\"ltr\">$record[student_payment_bank]</td>
<td dir=\"ltr\">$record[student_payment_type]</td>
<td dir=\"ltr\">$record[student_payment_date]</td>
<td dir=\"ltr\"></td>
</tr>\n";
}
but i dont know how to connect student_id and student_name and use in foreach because i have 2 rows of data.
(i'm a beginner in PHP / MySql)
Instead of querying database twice, you can instead join the tables to get the rows you want. Try to execute the query below in PhpMyAdmin or directly on MySQL Browser.
SELECT a.*, b.*
FROM student_info a
INNER JOIN student_payment b
ON a.student_ID = b.student_ID
-- WHERE ...if you have extra conditions...
ORDER BY b.student_payment_date DESC
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
It is possible to fix it with INNER JOIN, you can join 2 tables and use both values from 1 query.
http://www.w3schools.com/sql/sql_join_inner.asp
Or you can use the OOP way, not sure if that is what you need.
Make 2 objects from the 2 query's and put them in a foreach.
try this
$sql2 = " SELECT * FROM `student_info` WHERE student_id= '$student_id' ";
try this
$sql2 = "SELECT * FROM `student_info` WHERE student_id IN ($student_id)";
foreach($rows as $record ){
// i wanna to use student_name in first line
echo "\n<tr>
<td>$record[student_id]</td>
<td dir=\"ltr\">".$record['student_payment']."</td>
<td dir=\"ltr\">".$record['student_payment_id']."</td>
<td dir=\"ltr\">".$record['student_payment_bank']."</td>
<td dir=\"ltr\">".$record['student_payment_type']."</td>
<td dir=\"ltr\">".$record['student_payment_date']."</td>
<td dir=\"ltr\"></td>
</tr>\n";
}
Use Mahmoud Gamal code
But always select the needed columns only not all because
In future number of columns in table may increase which may decrease the performance of your application.
Also it may contain some important information not to be leaked.
It seems like you want a report of all payments made. The student name is to be displayed with the payment information. The results will probably have more than one payment per student.
This result is ordered by student name, and then payment date (most recent first)
SELECT s.student_name, sp.*
FROM student_payment sp
INNER JOIN student_info s ON s.student_ID=sp.student_ID
ORDER BY s.student_name ASC, sp.student_payment_date DESC
try to join the table and use single query instead of two -
$sql = "SELECT * FROM student_info, student_payment
WHERE student_info.student_id=student_payment.student_id"