Select from database without duplicate [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to develop php page but I have a problem, I would like to get data from data base without duplicate.
$tsql = "SELECT COUNT(ID) FROM FactoriesViolations";
$rowsPerPage = 25;
$stmt = sqlsrv_query($conn, $tsql);
please help me.
thanks in advance.

what all columns are you expecting in your output. If its only ID
$tsql = "SELECT COUNT(DISTINCT(ID)) FROM FactoriesViolations";
if you want all the columns from the table and eliminate the duplicate records
this query will do the needful.
SELECT Col1, Col2,... ColN FROM FactoriesViolations GROUP BY Col1, Col2,... ColN;
here Col1, Col2,... ColN are column names of your FactoriesViolations table.

Use below query. It will work.
$tsql = "SELECT COUNT(DISTINCT ID) FROM FactoriesViolations";

use below way for count of unique records
SELECT COUNT(DISTINCT column_name) FROM FactoriesViolations; // column_name is column which contains duplicate

DISTINCT keyword tells the server to go through the whole result set and remove all duplicate rows after the query has been performed.
Format :
SELECT DISTINCT *
FROM TABLE_NAME
WHERE CONDITION(S)
In your case, the following query should work
$tsql = "SELECT COUNT(DISTINCT(ID)) FROM FactoriesViolations" ;
This will return the count of all unique IDs existing in the table.

Related

How to skip counting null or empty cells inside MySQL? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have this database table where one column is email and in front of that have different other columns used to store data for that email. When I use mysqli_num_rows() function it counts the empty fields as well.
How should I modify this query such that I don't get empty cells counted
<?php
$query = "SELECT * FROM user_info WHERE email='$email'";
$data = mysqli_query($connec,$query);
$total = mysqli_num_rows($data);
echo "Registered for ". $total. " Modules";
?>
Thanking in anticipation
"SELECT * FROM user_info WHERE email='$email' and UID IS NOT NULL AND SENT_DATE IS NOT NULL AND TOTAL_BOUNCE_COUNT IS NOT NULL;"
For each column for which a NULL make the record invalid, put in that syntax to exclude records that have a null from the count. Of course I just made up column names in my answer. You'd put the column names in your table in place of SENT_DATE/TOTAL_BOUNCE_COUNT/UID.

Select all data from a second table where name is equal to a column value from first table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am working with a MySQL database wherein I have tables named as follows:
Users
1247483647
2147483613
2047483641
The columns in Users are as follows:
userEmail
compId
How can I structure an SQL query which will:
a) Select the compId from the Users table where userEmail is equal to an email address provided via a mysqli prepared statement.
b) Then find the table with a table name which matches the compId and select all data from it?
I have so far tried as follows (to no avail):
$sql = "
SELECT * FROM table_name
WHERE table_name =
(SELECT compId
FROM Users
WHERE userEmail = ?)
";
Thanks in advance.
You have to build the SQL string from the INFORMATION_SCHEMA.TABLES
select #querystring := CONCAT('SELECT * FROM ', table_name)
from information_schema.TABLES
where TABLE_NAME =
(SELECT
compId
FROM
Users
WHERE
userEmail = {?})
PREPARE statement from #querystring;
EXECUTE statement
I'm not the handiest with PHP but this works for me as long as you inject the userEmail in the {?} where I designated as a parameter.

Display/create a blank row in between data set and breack the table data using php/mysql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am using one table and two queries to display the below output.
Here I have used 1st query to display the morning/middle/evening and other to get the user details.
I want to user a empty/blanks row after the last morning/middle which need to have this output
Expected output:
Can anyone help me on this.
Not knowing your SQL statemanets, i would only advise to use
UNION ALL
(See Docu here)
So your Statement would look somehow like that:
Select Col1, Col2
from tbl1
UNION ALL
Select NULL as Col1, NULL as Col2
UNION ALL
Select Col1, Col2
from tbl2

Special keyword in mysql select statement [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Consider the following script:
SELECT * FROM TABLE1 WHERE COLUMN1='$exb';
$exb is a PHP variable which is fed from an HTML select list with 3 values:0,1,2. Values 1 and 2 correspond to column1 values. In case of selecting value 0, I want to include all the values of COLUMN1. Is there a way to implement the above without changing the script? In other words is there any keyword to assign to $exb which will oblige the script to select all the rows of table TABLE1 in case the user selects 0 from HTML select list?
Thank you
It's a little unclear, but I think you are asking is there a special clause you can add to a where clause to return every single row from the database rather than specific criteria matched in a where clause.
You can put in a pretend where clause by saying col1=col1 which is effectively a bogus (though valid) syntax like this:
SELECT * FROM TABLE1 WHERE COLUMN1=column1;
Though it would have to be without the quotes to select every single row of the table.
Putting the quotes around the variable would be very simple in your php however.
Having said that, wouldn't it be much easier to simply omit the where clause entirely if the value selected is 0?
For this you require to build a dynamic query.
$query = "SELECT * FROM TABLE1";
$exb = isset($_GET['exb']) ? $_GET['exb'] : 0;
$goodParam = array(0,1,2);//array of allowed values
if($exb>0){
if (in_array($exb, $goodParam)) {
$query .= " WHERE COLUMN1 = '$exb'";
}
}
echo $query;
I don't think you can do that with MySql query only. You need to use php and ifelse statement where you can check for $exb value before executing query. For example:
if($exb == "0")
$query = "SELECT * FROM TABLE1";
else
$query = "SELECT * FROM TABLE1 WHERE COLUMN1='$exb'";

How to count certain rows in a MySQL table? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I currently have a table that looks like this.
http://i.stack.imgur.com/KFP6Q.png
This is a comment system. the column id gives the comment an ID. the server_id is the ID for the section the comment was posted on. The user_id is the ID for the person who posted it. And lastly, the comment is the comment itself. Here is how I created the comment:
http://pastebin.com/VHUDW6Dm
What I want to do is create a variable, $commentcount, that will count how many comments there are for a server and be able to display them on a page. If someone could direct me to a function that can help me with this or actually create the code here, it would be greatly appreciated.
Since you want the number of comments per server, you can use the SQL GROUP BY clause to aggregate the resulting rows by the unique server_id.
SELECT server_id, COUNT(id) FROM comments GROUP BY server_id;
This will return the count for each server_id group. If you are only displaying this for a single server_id at a time, you can simply use
SELECT COUNT(id) FROM comments WHERE server_id = <your server id>;
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
A very rough draft for you would be to iterate a count.
$q = mysql_query("MYSQL QUERY TO GET ALL THE COMMENTS FOR THE USER");
$count = 1; // Start the count, preferrably at 1
while ($comment = mysql_fetch_assoc($q)) {
$count++; //iterate the count
}
echo $count; // Echo's the count;
use following mysql query
select count(*) as count_comment from comments;

Categories