How to get Max date with selected row id in Mysql - php

I have a scenario in which i want to get Maximum Date but not from Whole Table Just from Selected id but i failed.
Here is my table
I run This Query For get my selected id
SELECT * FROM `tbl_methodology` WHERE FIND_IN_SET(6, `col_select_corporate`)
and i got that result
From that result I want to get that record which have maximum Date
I try that query but it not working for me
SELECT * FROM `tbl_methodology` WHERE id IN (1,5,7) AND `col_date` = (
SELECT MAX(`col_date`)
FROM `tbl_methodology`)
Can anyone help me??

Add the where clause twice and you should get the result you expected:
SELECT * FROM `tbl_methodology` WHERE id IN (1,5,7) AND `col_date` = (
SELECT MAX(`col_date`)
FROM `tbl_methodology` WHERE id IN (1,5,7))
And as i commented: it is very bad db design, to save values as CSV

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'"

WHERE IN clause display result by them index in IN

i have a table, just id and name, have a mysql like this
`SELECT * FROM table WHERE id IN(154,12,148,50);`
and i use while loop PHP for display result as normal way :
while($rows= mysql_fetch_array($result)){ echo $rows['id'], echo $rows['name'] }
but result is ordered by id field in IN clause 12 ,50,148,154
i want to result still them index in IN() : 154,12,148,50
is there any way?
thank alot
read FIELD
SELECT *
FROM table
WHERE id IN(154,12,148,50)
ORDER BY FIELD(id,154,12,148,50)

Displaying specific information from 1 SQL SELECT query

I have a database table that contains a bunch of different options and their values. The table has 3 columns which are ID, menu_option, and value. So one row may have the following information: 1, "menu_color", "#AB324B".
I want to display the value of each option on the page so the user can edit the option. Right now, i'm creating a query to get the info for each specific option. Like so
SELECT * FROM menu_theme WHERE ID='1'
SELECT * FROM menu_theme WHERE ID='2'
SELECT * FROM menu_theme WHERE ID='3'
...
Instead of making a new query to get the info per row, how can I make 1 query and distinguish what row I want to get the data from and display the data using php?
I'm aware of how to use php while loops with an SQL query, but I can't see how that would work with selecting specific rows.
Maybe something like this
SELECT * FROM menu_theme WHERE ID IN ('1','2','3')
maybe something like this:
<?php
//select all the rows
$sqlSelect="SELECT ID,menu_option,value FROM menu_theme";
$result=mysqli_query($con,$sqlSelect);
while($row=mysqli_fetch_array($result))
{
$id=$row['ID'];
$opt=$row['menu_option'];
$val=$row['value'];
$menuId="input-".$id;
//create the label for the input
echo "<label for='".$menuId."'>".$opt."</label>";
//pre-populate the input with the name,value,id
echo "<input type='text' name='".$menuId."' id='".$menuId."' value='".$val."'/>";
}
?>

How to display count information to page

I have created a query to select and count specific fields from one table and the corrisponding name from another table the query works when i run it in the sql on localhost but cant work out how to display the result to the page
e.g
Item name1 - count
tiem name2 - count
here is the query
$objects->connect();
// Count Query
$countQuery = 'SELECT a.`subId`, b.`subId`, b.`subTitle`, COUNT(a.`subId`) FROM `tbl_list`a, `tbl_subs`b WHERE a.`subId` = b.`subId` GROUP BY a.`subId`';
//Run the query
$objects->query($countQuery);
how would i display the result to the page
First your function $objects->query($countQuery); must return the result array which you can get like this:
$result = $objects->query($countQuery);
and then apply loop to $result;
Yes add COUNT(a.subId) as cnt as well and then like echo $result[0]->cnt;
depends upon your return array format.

Categories