I have 6 Records in following Table
I Need to get Unique Leads id Columns Order by id DESC.
Following is my Query
Telecallerfirststep::WhereDate('created_at',date('Y-m-d'))->groupBy('leadsid')->OrderBy('id','DESC')->count();
Its Return Number of Records is : 1
Just Add distinct
Telecallerfirststep::WhereDate('created_at',date('Y-m-d'))-
>groupBy('leadsid')->OrderBy('id','DESC')->distinct()->count();
You should try this:
Telecallerfirststep::whereDate('created_at','=',date('Y-m-d'))->groupBy('leadsid')->count();
Use php count after fetching result
$reault = Telecallerfirststep::whereDate('created_at',date('Y-m-d'))->groupBy('leadsid')->OrderBy('id','DESC');
echo $count = count($reault);
I found similar issue MySql and SO
Related
i query to check if a point(input) is intersect with polygons in php:
$sql1="SELECT ST_intersects((ST_SetSRID( ST_Point($startlng, $startlat),4326))
, zona_bahaya.geom) as intersek
FROM zona_bahaya";
$query1 = pg_query($conn,$sql1);
$check_location = pg_fetch_array($query1);
if (in_array('t',$check_location)) {
dosemthing1;}
else {dosomething2;}
it's work peroperly before i update the data
after data updated, it's only show the first row when i check the pg_fetch_array result. here is the result {"0":"f","intersek":"f"} .
i try to check from pgadmin and it's can show 8 result (1 true(intersected) and 7 false(not intersect)) using updated data with this query:
SELECT ST_intersects((ST_SetSRID( ST_Point(110.18898065505843, -7.9634510320131175),4326))
, zona_bahaya.geom) as intersek
FROM zona_bahaya;
to solve it, i order the query result descended so the 'true' gonna be the first like this:
order by intersek desc
anybody can help me to findout way it just only show the first row???
here some geom from STAsText(zonabahaya.geom) not all of them : MULTIPOLYGON(((110.790892426072 -8.19307615541514,110.791999687385 -8.19318330973567,110.794393723931 -8.1927980624753,110.794586347561 -8.19205508561603,110.795329324421 -8.19120203811094,110.796540101525 -8.19023891996003,110.797503219676 -8.18933083713203,110.798576408472 -8.18919324882476,110.79929186767 -8.18957849608512,110.800337538805 -8.19059664955894,110.800585197758 -8.19150473238694,110.80022746816 -8.19238529755349,110.799787185576 -8.19290813312112,110.799589319279 -8.19300706626968,110.798788231202 -8.19299429992581,110.798537293576 -8.19311976873883,110.79850269889 -8.1933090511224,110.798620939451 -8.19433728092441)))
In order to filter only the records that intersect you have to use ST_Intersects in the WHERE clause:
SELECT *
FROM zona_bahaya
WHERE ST_Intersects(ST_SetSRID(ST_Point(110.18, -7.96),4326),zona_bahaya.geom);
Since you're dealing with points and polygons, perhaps you should take a look also at ST_Contains.
In case you want to fetch only the first row you must set a limit in your query - either using LIMIT 1 or FETCH FIRST ROW ONLY -, but it would only make sense combined with a ORDER BY, e.g.
SELECT *
FROM zona_bahaya
JOIN points ON ST_Intersects(points.geom,zona_bahaya.geom)
ORDER BY gid
FETCH FIRST ROW ONLY;
Demo: db<>fiddle
I was working with a project where I want to get the count of values in a column. I will explain with an example.
I have a table column named name. Where it's values are A,B,C,D,A,D,B,A,C.
Now I want the output as
Count: A-3,B-2,C-2,D-2.
I have tried using group by and distinct. but both don't give me what I want. It all getting the total count of that item. In the code given below is the query I tried. There I want the count of particulars_id and the $public_page_id will be common for all particulars_id I am fetching. There will be a number of public_page_id in the table, and each will have some particulars_id under them.
$output = '';
$this->db->select('COUNT(service_appointment_details.particulars_id)
as count,particulars.particulars_name');
$this->db->from('particulars');
$this->db->group_by('particulars.particulars_id');
$this->db->where('particulars.public_page_id',$public_page_id);
$this->db-
>join('service_appointment_details','particulars.public_page_id =
service_appointment_details.public_page_id','right');
$this->db->where($where_date);
$query = $this->db->get();
Expected Result
Expected Result is (based on the above example)
Count: A-3,B-2,C-2,D-2.
Actual Result
But what I'm getting now is
Count: A-9,B-9,C-9,D-9.
I need to fetch count of each particulars_id under the given public_page_id
You should use count(*)
$this->db->select('COUNT(*) as count,particulars.particulars_name');
........
As pointed out by #scaisEdge, you need to use count(*) instead of count(service_appointment_details.particulars_id). By using count(service_appointment_details.particulars_id), you basically count the number of rows from your select which is not what you want.
Final snippet would be:
$output = '';
$this->db->select('COUNT(*) as count, particulars.particulars_name');
$this->db->from('particulars');
$this->db->group_by('particulars.particulars_name');
$this->db->where('particulars.public_page_id',$public_page_id);
$this->db->join('service_appointment_details','particulars.public_page_id = service_appointment_details.public_page_id','right');
$this->db->where($where_date);
$query = $this->db->get();
Whenever you want to count occurrence of a column values, you'd do
SELECT column_to_count, count(*) FROM table GROUP BY column_to_count
Hello I have been working on an sql code I found online that said that it check if there duplicates online.
What I want to do is that it checks whether if a value has a duplicate in the table it is inserting then I need to return a booleon in PHP but I am having a problem in MYSQL code. I have this code I have found online:
SELECT schedday,schedtime COUNT(schedday, schedtime) as count
FROM scstock
GROUP BY schedday, schedtime
HAVING COUNT(schedday, schedtime) > 1
But I am having this error
Then I tried to modify it to this which is I wanted to accomplish
SELECT schedday,schedtime COUNT(schedday, schedtime) as count
FROM scstock
WHERE schedday = 'M/T' AND schedtime = '7:00-9:00/7:00-9:00'
HAVING count > 1
But this appears
Can you help me?
Try this below query for your result
SELECT schedday,schedtime, COUNT(*) as count FROM scstock GROUP BY schedday, schedtime HAVING count > 1
you have syntax error you should put an "," after schedtime
SELECT schedday,schedtime , COUNT(schedday, schedtime) as count
FROM scstock
GROUP BY schedday, schedtime
HAVING count > 1
I'm trying to add a single column in a db query result. I've read about the SUM(col_name) as TOTAL, GROUP BY (col_name2).
But is there a way i can only SUM the column without any GROUPing? I a case whereby all col_name2 are all unique.
For example... I have a result with the following col headers:
course_code
course_title
course_unit
score
grade
Assuming this have 12 rows returned into an HTML table. Now i want to perform SUM() on all the values (12 rows) for the column course_unit, in other to implement a GPA school grading system.
How can i achieve this.
Thanks.
SELECT SUM(col_name) as 'total' FROM <table>
GROUP BY is required only if you want to sum subsets of the rows in the table.
You can find sum or any aggregate db functions (such as count, avg, etc) for most cases without using group clause. Your sql query may look something like this:
SELECT SUM(course_unit) as "Total" FROM <table_name>;
As comments below have already pointed out: SELECT SUM(course_unit) AS total FROM your_table;. Note that this is a separate query to the one with which you retrieve the table data.
This does it in php. I'm not sure how to do it with pure sql
$query = "SELECT * FROM table";
$result = mysql_query($query);
$sum = 0;
while($row = mysql_fetch_assoc($result))
{
$sum+= intval($row['course_unit']);
}
echo $sum;
SELECT
course_code,
course_title,
course_unit,
score, grade,
(select sum(course_unit) from TableA) total
from TableA;
If I need to know the total number of rows in a table of database I do something like this:
$query = "SELECT * FROM tablename WHERE link='1';";
$result = mysql_query($query);
$count = mysql_num_rows($result);
Updated: I made a mistake, above is my actual way. I apologize to all
So you see the total number of data is recovered scanning through the entire database.
Is there a better way?
$query = "SELECT COUNT(*) FROM tablename WHERE link = '1'";
$result = mysql_query($query);
$count = mysql_result($result, 0);
This means you aren't transferring all your data between the database and PHP, which is obviously a huge waste of time and resources.
For what it's worth, your code wouldn't actually count the number of rows - it'd give you 2x the number of columns, as you're counting the number of items in an array representing a single row (and mysql_fetch_array gives you two entries in the array per column - one numerical and one for the column name)
SELECT COUNT(*) FROM tablename WHERE link='1';
You could just do :
SELECT count(*) FROM tablename;
for your query. The result will be a single column containing the number of rows.
If I need to know the total number of rows in a table of database
Maybe I'm missing something here but if you just want to get the total number of rows in a table you don't need a WHERE condition. Just do this:
SELECT COUNT(*) FROM tablename
With the WHERE condition you will only be counting the number of rows that meet this condition.
use below code
$qry=SHOW TABLES FROM 'database_name';
$res=mysql_query($qry);
$output=array();
$i=0;
while($row=mysql_fetch_array($res,MYSQL_NUM)){
++$i;
$sql=SELECT COUNT(*) FROM $row[0];
$output[$i]=mysql_query($sql);
}
$totalRows=array_sum($ouptput);
echo $totalRows;
http://php.net/manual/en/function.mysql-num-rows.php You need this i think.
If you are going to use the following SQL statement:
SELECT COUNT(*) FROM tablename WHERE link='1';
Make sure you have an index on the 'link' column