How can I limit the display to distinct records? - php

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
");

Related

Php grouping select query

So I have persons associated with publications
My current query is this:
$query = "SELECT confAuth.personid, publicationconf.title FROM confAuth INNER JOIN publicationconf ON publicationconf.conferenceid = confAuth.conferenceid GROUP BY publicationconf.conferenceid";
It does group by conference id but only one person id is diplayed for each entry. Obviously, what I need is to display all person id's
Thanks in advance.
As a classmate just noted, this is our print statement and it is probably wrong. Any input to this.
$result = $conn->query($query);
$rows = $result->num_rows;
if ($rows > 0)
{
for($i=0; $i<$rows; $i++)
{
if($row = $result->fetch_assoc())
{
?>
<div class="row">
<div class="col-md-10">
<p><?php echo $row["personid"] . ", " . $row["title"]?></p>
Use group_concat(confAuth.personid) to get a comma delimited list of all values in the grouped set.
However, since you are wanting one line item per personid/title combo, add the personid and the title to the group by clause.
Also, consider table aliases for readability.
$query = "SELECT ca.personid, pc.title
FROM confAuth ca
INNER JOIN publicationconf pc ON pc.conferenceid = ca.conferenceid
GROUP BY pc.conferenceid, ca.personid, pc.title";

Count column of table group by rows

I can count number of a table rows by using mysqli_num_rows. I have a table which contains similar rows. How can I count by grouping by similar row?
For instance: I have a table with 2 columns : Student and Option. Let say there are 50 students. 20 are in Economy option, 20 are in Management option and 10 are in Secretary Option. How can display those numbers. I can display only the 50.
my codes
$qry = "select * from table group by option";
$req= #mysqli_query($conn, $qry);
$result = mysqli_query( $conn, "select id from table");
$num_rows = mysqli_num_rows($result);
Students Total (<?php echo $num_rows ?>)
<table >
<tr>
<th>Student</th>
<th>Option</th>
</tr>
<?php
while($row=mysqli_fetch_array($req))
{
?>
<tr>
<td><?php echo $row['student'] ?></td>
<td><?php echo $row['option'] ?></td>
</tr>
<?php
}
?>
</table>
Here is the query you need:
SELECT COUNT(*) AS `option_count`, * -- returns a count aliased as "options_count"
FROM `table`
GROUP BY `option` -- will group the options and show the counts
Now you can echo the count, along with other data:
echo $row['count_options'];
echo $['options'];
The problem that you have here is that you will not be able to display each student in the option because this counts / groups only the three options.
Behold the proper query :
$qry = "select option as opt, COUNT(*) AS option from table group by option";

DISTINCT - download the content again

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.

i want to count specific IDs from my tables

i have this code
<?php
$votes_query=mysql_query("select * from votes where CandidateID='$id'");
$vote_count=mysql_num_rows($votes_query);
echo $vote_count;
?>
which fetches individual *ID*s from my votes table.
I need a code which can help fetch the sum of specific *ID*s.
Lets say, I have ID-205 and ID-209 in my table.
<?php
$ids = array('205','209');
$votes_query=mysql_query("select * from votes where CandidateID IN($ids) ");
$vote_count=mysql_num_rows($votes_query);
echo $vote_count;
?>
Put all ID's in an array & Use IN Clause of mysql.
<?php
$ids = array('205','209');
$sql "SELECT SUM(field_score) AS vote_score FROM votes WHERE CandidateID IN ($ids)";
$vote_score = mysql_query($sql);
echo $vote_score;
?>

Mysql querys over multiple tables

This code pulls together a list of members then lists all the items they have added in a category and then outputs all this information to a table.
Here's the 3 table layouts:
Members
member_id - fname - lname - etc.
Items
member_id - item_id - etc.
Categories
name - etc.
And the current code which takes FOREVER to load:
<?php $sql="SELECT * FROM members ORDER BY lname, fname ASC";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){ ?>
<? echo('Name'); ?>
<? echo $rows[fname];?> <? echo $rows[lname];?>
<? $sql2="SELECT * FROM leadtypes ORDER BY name ASC";
$result2=mysql_query($sql2);
while($rows2=mysql_fetch_array($result2)){ ?>
<? echo $rows2[name];?>
<? echo(': '); ?>
<? $sql4="SELECT lead_id FROM leads WHERE member_id='$rows[member_id]' AND type='$rows2[name]' ORDER BY name ASC";
$result4=mysql_query($sql4);
$num4 = mysql_num_rows($result4); ?>
<? echo $num4;?>
<? } ?>
<? } ?>`
I know I shouldn't query * but this is a very stripped down piece of code on a test server. I would be grateful If somebody knows a good way of combining all this together to speed up the system.
Try this
$sql = SELECT A.lead_id,B.name,C.fname,C.lname
FROM leads A
JOIN leadtypes B ON A.type = B.name
JOIN members C ON A.member_id = C.member_id
You can echo this as
$rs = mysql_query($sql);
while($rows = mysql_fetch_array($rs)){
echo $row['name'];
echo $row['fname'];
}
Have a look at these links
http://www.w3schools.com/sql/sql_join.asp
http://dev.mysql.com/doc/refman/5.0/en/join.html
Try doing one join instead of multiple queries. For example,
SELECT lt.*, l.lead_id FROM leadtypes lt, leads l WHERE l.member_id=lt.member_id AND l.type=lt.name ORDER BY l.name ASC
If that one query is still slow then it means you need to add indices on your tables.

Categories