Checking if there are duplicates in a tables - php

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

Related

php pg_fetch_array only show first result

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

Count function in mysql

There are 2 table called employee and approvedcadre.I want to get count of raws where
employee.Des_name=approvedcadre.Des_name
AND employee.service=approvedcadre.service
AND employee.Grade=approvedcadre.Grade
AND employee.SalaryCode=approvedcadre.SalaryCode
WHERE app_category='permanent'"
FROM approvedcadre
WHERE Ins_name='$InsName'
I herewith attached the current code.but I don't know how to apply "SELECT * FROM approvedcadre WHERE Ins_name='$InsName' to the query?
[Above mentioned code]
try this code it for count the rows
in php use * and use mysqli_num_rows
SELECT COUNT(*)
FROM approvedcadre
JOIN employee ON employee.Des_name=approvedcadre.Des_name
AND employee.service=approvedcadre.service
AND employee.Grade=approvedcadre.Grade
AND employee.SalaryCode=approvedcadre.SalaryCode
WHERE approvedcadre.Ins_name='$InsName' and approvedcadre.app_category='permanent'"

How to count entry by refrenceName?

How to count entry by refrenceName?
my sql query is
SELECT GROUP_CONCAT(refrenceName)
FROM attendance
where event='4'
GROUP BY refrenceName
HAVING ( COUNT(refrenceName) > 0 )
and showing result below
but how to count? I want Ashish(6), Babli(1)Rebecca(5) pls help me
SELECT concat(refrenceName,"(",count(refrenceName),")")
FROM attendance
where event='4'
GROUP BY refrenceName
HAVING ( COUNT(refrenceName) > 0 );
If you use GROUP_CONCAT for reference name, it will show result as reference name with comma separated values
(like : Ashish,Ashish,Ashish,Ashish,Ashish,Ashish)
As per your expected output, you want to show only the name-not to be duplicated in the result, along with count.
So Hereby concatnating the referenceName with count(referenceName) as we have already groupped by the same column referenceName.

How to get Distinct rows in Laravel

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

Does't selecting the good value in sql

Hello I have a sql syntax and it always take the last record and doest check the other condition.
SELECT *
from projetstaches ,users,timesheets
WHERE `prtTimeSheetId` = ( SELECT MAX( `prtTimeSheetId` ) FROM projetstaches ) AND usrId = 16 AND timId = prtTimeSheetId
I'm working with php and sql but I know this is my syntax is not good.
It's always give me my last record . It's do not take the last record of my user 16 . Cause my last record its for my user 7 . Have any idea why?
So I need to take the last projettime sheet of my user 16
**EDIT **
here what look like my data http://pastebin.com/6LBwGtc3
I suppose your query should look like this:
SELECT *
from
projetstaches
inner join
timesheets
on (timesheets.timId = projetstaches.prtTimeSheetId)
inner join
users
on (users.usrId = timesheets.timUserId)
WHERE
users.usrId = 16
order by timesheets.timId desc
limit 1
To understand how it works I suggest you to play with this query:
remove where - check result
remove limit - check result

Categories