In my module there will be a link that displays the counted duplicate data of every user.
Here is my query for counting duplicates of each user.
if ($_POST['submit']){
$query=$db_operation->query("SELECT `lname`,`fname`,`mname`,`bday`,`gender`, COUNT(*) c FROM `user` WHERE `status` = 'ACTIVE' GROUP BY `lname`,`fname`,`mname`,`bday` HAVING c > 1 LIMIT 10"); }
Below is the sample output of count for each user:
Output
Now, using the link for another page view. I need to view the rows counted by the duplicate data.
Below is the sample output for the counted rows:
Output
I am using php language and MYSQL for database.
What query should I use to view the rows of count duplicate?
All you need to do is fetch the associative array of the duplicate rows and loop over them.
$query = mysql_prepare($db_operation, "SELECT `lname`,`fname`,`mname`,`bday`,`gender`, FROM `user` WHERE `status` = 'ACTIVE' GROUP BY `lname`,`fname`,`mname`,`bday` HAVING COUNT(*) > 1 LIMIT 10"
$query->execute();
$query->get_result();
$query->fetch_assoc();
var_dump($query);
Change to suit your needs, this is just pseudo code.
Related
I wanted to fetch data from MySQL with PHP when a user inputs something. I have this code.
$query = "SELECT * FROM mealplans WHERE plantitle LIKE '%$inputText%' AND `STATUS` = 1 OR plantitle LIKE '%$inputText%' AND userid = '$userid' GROUP BY plantitle, userid";
$result = mysqli_query($con, $query);
It works fine however, it fetches the oldest or first data from the group. How do I select the newest data?
I've search and found possible solutions by using FROM, JOIN, IN, etc. However, I do not know how to use them since I only know the basics. Hopefully someone could explain one solution for me.
There is a simple solution that you can do.
You can order your data to DESC order. I'm sure that you have an id(primary key) in your table.You just have to order all your datas' to DESC order of ids .So that your last inserted data set will be the one to be fetched since it's on the top.
change your query to: (add your primary key(in the following query i have added it as id) in the ORDER BY syntax)
$query = "SELECT * FROM mealplans WHERE plantitle LIKE '%$inputText%' AND `STATUS` = 1 OR plantitle LIKE '%$inputText%' AND userid = '$userid' GROUP BY plantitle, userid ORDER BY `ID` DESC"
Just add a condition in your query which only retrieves the row having the greatest id.
Find out the id first and then you can use the same query for that id:
SELECT MAX(id) FROM mealplans WHERE plantitle LIKE '%$inputText%' AND `STATUS` = 1 OR plantitle LIKE '%$inputText%' AND userid = '$userid' GROUP BY plantitle, userid
After getting the id and storing it in a variable (say $temp):
SELECT * from mealplans where `id` = $temp
i have a small problem. I use two mysql queries for getting data.
First i want to get IDs from groups
$sqlGoups = "SELECT * from `groups` WHERE `Date`='$TodayDate' ";
$result = $conn->query($sqlGoups);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$IDgroups = $row["ID"];
With that, I'll get those IDs, for example 5, 7, 12, 15, 22
I want to put them all in the next mysql query:
$sqlNext = "SELECT * FROM `orders` WHERE ID = '$IDgroups' ORDER BY `ID` ASC ";
$result = $conn->query($sqlNext);
When I do this, I get the result only for the first ID (5). And I want for each
I can not INNER JOIN tables because I use this in next query.
I tried with foreach loop, but no effect.
Try this code
SELECT * FROM `orders`
WHERE ID REGEXP CONCAT('(^|,)(', REPLACE('$IDgroups', ',', '|'), ')(,|$)')
ORDER BY `ID` ASC
Just like #Elanochecer commented the best bet should be a JOIN statement, but if you wish to go through your route, you could use the IN and provide the IDs as comma separated string, your query should look similar to the one below:
...
$sqlNext = "SELECT * FROM orders WHERE ID IN ('$IDgroups') ORDER BY ID ASC ";
...
Also, confirm if $IDgroups is in the format 1,2,3,4
If you provide the schema I could come up with a workable JOIN statement for you, preferably you can create a repo with the schema
have a table with a "tag" column - I want to select 10 random records from my table where none of the ten records share the same tag (each record has a unique tag). How would I do this? This is my current query:
$this->db->select('extra_imagery');
$this->db->where('cat', '4');
//need something like: $this->db->where('tag',IS UNIQUE);
$this->db->limit($this->config->item('imagery-limit'));
$this->db->order_by('extra_imagery.id','RANDOM');
Try this
$query = $this->db->query("SELECT DISTINCT extra_imagery FROM table_name WHERE cat = 4 GROUP BY RAND() id");
$result = $query->result_array();
return $result;
In order to avoid repeats, you can use DISTINCT and/or a GROUP BY to your query.
As per what the manual states:
https://ellislab.com/codeigniter/user-guide/database/active_record.html
For example:
$this->db->distinct();
and/or
$this->db->distinct();
Other references (MySQL):
http://dev.mysql.com/doc/refman/5.7/en/distinct-optimization.html
http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html
http://dev.mysql.com/doc/refman/5.7/en/select.html
Very new to PHP and JSON, I'm using the following to display a list of rows:
$db = new PDO('mysql:host=localhost;dbname=rugbysuperleague','xxx','xxx');
$stmt = $db->query("SELECT * FROM `table`");
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->execute();
echo json_encode($data);
It returns all the rows in the table OK, But how do I return the rows sorted based on aAscending values of a specific column, if tie, sort by next column. etc.
I'm trying to list in correct order a Sports Team Standings table. So that the team with most points is in 1st position, if there is a tie, sort by point difference etc.
Use ORDER BY and list the the columns in order of how you want it ordered:
SELECT * FROM `table` ORDER BY `points` DESC, `point_diff` ASC
You also don't need execute() and probably not the fetchAll().
I have a table as below,
ID Name Age
----------------------
100 A 10
203 B 20
Now how do i select only row1 using MySQL SELECT command and then I've to increase +1 to it to select row2. In short I'll be using for loop to do certain operations.
Thanks.
Sounds like you've got a mix up. You want to select all the rows you want to iterate through in your for loop with your query, and then iterate through them one by one using php's mysql functions like mysql_fetch_row
You should not try to use tables in a linear fashion like this. Set your criteria, sorting as appropriate, and then to select the next row use your existing criteria and limit it to one row.
SELECT * FROM `table` ORDER BY `ID` LIMIT 1
SELECT * FROM `table` ORDER BY `ID` WHERE ID > 100 LIMIT 1
You'd probably be better off retrieving all rows that you need, then using this. Note the LIMIT is entirely optional.
$query = mysql_query(' SELECT ID, Name, Age FROM table_name WHERE condition LIMIT max_number_you_want '))
while ($row = mysql_fetch_assoc($query)
{
// Do stuff
// $row['ID'], $row['Name'], $row['Age']
}
Lots of small queries to the database will execute much slower than one decent-sized one.
You should get the result into an array (php.net : mysql_fetch_*).
And after you'll can loop on the array "to do certain operations"
Yep, this is a pretty common thing to do in PHP. Like the others who have posted, here is my version (using objects instead of arrays):
$result = mysql_query("SELECT * FROM table_name");
while ($row = mysql_fetch_object($result)) {
// Results are now in the $row variable.
// ex: $row->ID, $row->Name, $row->Age
}