This question already has answers here:
Using Count to find the number of occurrences
(4 answers)
Closed 1 year ago.
Given Database Table
Expected Output
No of Occurrence of A
No of Occurrence of B
No of Occurrence of C
...
using PHP
Actually this is something you would solve much more easily using SQL, not PHP:
SELECT
Agent,
COUNT(Agent)
FROM
yourtable
GROUP BY
Agent
Results:
Agent COUNT(Agent)
--------------------
A 4
B 4
C 2
Demo: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=86482e0d543a21ed7b0a22d7ab498e7e
This is called an "aggregate query", where you group the results by a specific column and the form a function on those results.
More info about COUNT and GROUP BY can be found in the MySQL documentation, and numerous SQL tutorials etc around the web.
This question already has answers here:
Returning query results in predefined order
(11 answers)
Closed 6 years ago.
I have a IN clause query in which I pass this ID'S (10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29)
My query is,
SELECTpost_jobs.idFROMpost_jobsWHEREidIN(10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29)
It execute correctly and returns result first id likes 1 then 4 then 7 and so on...that is ascending order
My Need is to get first result of id 10 then 19 then 23 as the order of ID that I passed is there any way to get such type of result in output.
Try this
SELECT post_jobs.id FROM post_jobs WHERE id
IN(10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29) ORDER BY
FIELD(id,10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29);
In Codeigniter
$this->db->_protect_identifiers = FALSE;
$this->db->select('id');
$this->db->where_in('id',array(10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29));
$this->db->order_by('FIELD(id, 10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29)');
$this->db->get('post_jobs');
If you are using codeigniter 3 then write or remove the line of protect_identifiers
$this->db->protect_identifiers(False);
Try this one
SELECT post_jobs.id FROM post_jobs WHERE post_jobs.id
IN(10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29) ORDER BY
FIND_IN_SET(post_jobs.id,10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29);
This question already has answers here:
MySQL pagination without double-querying?
(9 answers)
Closed 7 years ago.
As I know a pagination structure requires minimum two sql query.
First, find total row
Second, limit your query.
Is there a way to decrease query to one. Can we use first sql query to manupulate the pagination? On first query we already fetch all necessary data. Is first query's array can handle this issue?
You can do a simple previous/next pagination with a single query.
For this i your result limit is say 25, just query for 26 and only display the 25. If you get back less than 26 results, you know you don't have any more.
However if you are wanting to accurately display links for page 1,2,3,etc.. you have to do both a query for the total number of records in the table, and a query for just the data you want to display.
This question already has answers here:
How to put more than 1000 values into an Oracle IN clause [duplicate]
(11 answers)
Closed 9 years ago.
I am using an IN clause, whereby, it needs to search the id IN these set of IDs that are not in order, meaning, it came from a different criteria., how to solve this ?
SELECT DISTINCT ID FROM "projeck"."mytable" "t" WHERE staffID IN (75953,196262,196387,133585,195639,196702,195790,195820,192903,145383,179603,175896,176554,43545,154843,183798,195767,195715,etc..etc.. etc..)
and i am getting this oracle error
General error: 1795 OCIStmtExecute: ORA-01795: maximum number of expressions in a list is 1000
My first choice would be to reference the function that generates these values directly.
If the values were being used for multiple queries and were expensive to calculate then I'd think about loading them into a global temporary table and joining to it.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get Insert id in MSSQL in PHP?
I MYSQL I would do
SHOW TABLE STATUS LIKE 'image'
In mssql, sigh, I've no idea how to do it...
Please advice.. :)
You can try this to get the NEXT identity.
SELECT IDENT_CURRENT('image') + IDENT_INCR('image')
You can use this:
SELECT IDENT_CURRENT ('image')
then add 1 to get the next.
It's better than using max(id)+1 because the latest id might have been deleted
You could use the max value +1. Something like
Select max(id)+1 from image;