I'm having trouble with one of my MySQL queries:
SELECT id,Portfolio,Agency,Program,Objective,billion1,value11_12,
Proportion1,billion,value12_13,Percentage,Difference,
Diff_Percent,PIT,TOS,Actual_PIT,Actual_TOS,SUM(billion),SUM(Percentage),
SUM(PIT),SUM(TOS),SUM(Actual_PIT),SUM(Actual_TOS)
FROM budget_table
GROUP BY 'sum(Billion)'
WITH ROLLUP
LIMIT 0,100
The query works but I can't get the limit to work with the rollup. You can see the results of the query at BudgetAus
It is totalling all results rather than the first 100 which is what I am now trying to achieve. I have also tried just using LIMIT 100 but can't get anything to work. I tried using EXPLAIN (for the first time) in MySQL on the database and it said that the query executed although I didn't see any results- but as I've never used it before I wasn't sure what to look for. I'm new to programming and use WAMP rather than a CLI.
I'm also not sure whether to start my limits from 0 or 1?
GROUP BY 'sum(Billion)'
This does not group by each distinct value of the Billion column. This groups by a single value, the constant string literal 'sum(Billion)'. So you'll get one group, because all rows have the same value when you use a constant string literal as your grouping expression.
Take the quotes off when you put an expression in the GROUP BY column.
Then you'll find that this is not a valid expression for grouping anyway.
SUM() operators can't be used in the GROUP BY expression, as SUM() is an aggregate operator, and hence makes no sense inside a GROUP BY
Therefore you should be using id, or some other field, for the GROUP BY.
The second parameter to LIMIT (the offset parameter) starts at 0, not 1.
My son (who is around here somewhere but I don't know his username) decided he wanted to solve this for me today and provided the following solution:
//Get the main result set
$result = mysql_query("SELECT * FROM budget_table ORDER BY Percentage DESC LIMIT 0,100 ");
//Build an array of the ids for the result set
$ids = array();
$rows = mysql_num_rows($result);
//Get id for each row and add to the ids
for($i = 0; $i < $rows; $i++) $ids[] = mysql_result($result, $i, "id");
//Join the ids into a string for use with IN()
$ids_str = implode(",", $ids);
//Get the sums for only the ids of the resultset above
$result2 = mysql_query("SELECT SUM(billion),SUM(Percentage),SUM(PIT),SUM(TOS),SUM(Actual_PIT),SUM(Actual_TOS) FROM budget_table WHERE id IN($ids_str)");
Related
I have a table with this type of value 20|10|5|8|19| (with separator)
I need to select rows, where first value (for example after explode), less than 20.
$arr = explode("|", "goal_times");
$first_goal_time = $arr[0];
But how to do this in Mysql query?
In general you shouldn't have multiple values with separator in the same column. In this case you can get away with SUBSTRING_INDEX()
SELECT *
FROM yourtable
WHERE
SUBSTRING_INDEX(yourcolumn,'|',1) < 20;
I am wondering if its possible to add fields to the top of the table instead at the end.
So that when I do a query, its always getting the most recent fields and that the id( 1 ) is always at the end of the table and the most recent id is always at the top of page 1 of the results.
It sounds like you're actually asking about rows, not fields.
If you want the results of a query to come in a specific order, use the ORDER BY clause in your SELECT query. So if you want the highest ID first, use ORDER BY id DESC.
When you don't use ORDER BY, the results can be in any aribtrary order.
$q = 'SELECT * FROM list WHERE userid = '.$userid.' ORDER BY id DESC'; // should help you.
$result = mysqli_query($con, $q) or die(mysqli_error($con));
I am trying to create a simple pagination but it seems to fail to limit the number of results.
SELECT * FROM visits GROUP by clientID ORDER BY 'date' LIMIT $from, $to
I want to get only the first visit (chronologically) of every client and paginate the results.
If I make this query with $from = 6, $to = 12 it returns like 8 rows instead of 7.
What I'm doing wrong?
From MySQL's docs on LIMIT clause
The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).
With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1).
When you chose $from as 6 and $to as 12; you are not selecting from 6 to 12; you will be selecting 12 rows, starting from $from + 1 = 7.
The first argument to the LIMIT cluase is the starting offset and the second is the number of rows to return.
Therefore your query should be:
... LIMIT $from, ($to - $from)
With the dbms and syntax you're using, the second number in the limit expression is the number of results allowed, not offset endpoints. So you're starting at 6 and allowing the next 12 results, not getting results 6-12.
If you want to get results 6-12, use limit 5,7
Your query does not return the first visit by date. To do this, you need to actually join in this information:
select v.*
from visits v join
(select clientid, MIN(date) as mindate
from visits
group by clientid
) vd
on v.clientid = vd.clientid and v.date = vd.date
order by clientid
limit $from - 1, $to - $from
Your originally query is returning an arbitrary set of columns about a given client. These columns are not even guaranteed to be from the same record. This is because you are using a MySQL (mis)feature, where you can have columns in the select clause that are not in the group by clause and are not the arguments to an aggregation function.
I'm trying to do a query based on specific numbers which at this stage I only need to hardcode into the page as comma separated values.
2312,2431,2439,2440,2442,....
But I can not use between because the numbers in between may not be relevant
So how do I do a query of this type?
$sql= "SELECT * FROM table
WHERE pc=2431 OR pc=2439 OR
pc=2440 OR pc=2442 OR
pc=2443 OR pc=2444 OR
pc=2445 OR pc=2446 OR
pc=2312 AND v=1
ORDER BY id DESC
LIMIT $offset, $rowsperpage";
I tried this and kind of works but there must be a better way.
Thanks in advance
Indeed there is... You can use the IN operator so that the query becomes:
SELECT *
FROM table
WHERE pc IN (2431,2439,2440,2442,2443,2444,2445,2446,2312)
AND v=1
ORDER BY id DESC
LIMIT $offset, $rowsperpage
Simples :o)
For more info check out the MySQL documentation at http://dev.mysql.com/doc/refman/5.5/en/comparison-operators.html#function_in
I would suggest a little improvement to other answers:
$pcs = array(2431,2439,2440,2442,2443,2444,2445,2446,2312);
"SELECT *
FROM table
WHERE pc IN (" . implode(",", $pcs) . ")
AND v=1
ORDER BY id DESC
LIMIT $offset"
this way if later your ids will change and you have several queries using them you will have to update only one place instead of N (where N is number of queries). If you use these ids in many different files, consider making a constant
Seeing some strange things; help is being solicited.
I have a query, like so: (using CodeIgniter, btw)
'SELECT * FROM registers WHERE client_id='.$clid .' ORDER BY date DESC LIMIT '.$num
$num is passed in through the function call (and it==15), and is echoing properly. But running this returns only 10 rows. If I explicitly set $num to 15, same thing. If I set $num to 20, it returns 11 rows! WTF's in my dome!
Howeverstance, If I set the order to ASC, instead of DESC, the original query runs as expected.
The query returns the expected number of rows when run in CocoaMySQL with either sort.
Any idears?
It's returning the proper number of results. I had a clause that was preventing the rows from printing under certain conditions, and they were being met. Checked num_rows() and it was correct, so found the offending code.
$this->db->select('*');
$this->db->from('registers');
$this->db->order_by('id','DESC'); //client_id='.$clid
$this->db->limit(4); //'.$num
$query_result = $this->db->get();
$hasil = $query_result->result();
return $hasil;