Mysql query based on a list of specific random numbers? - mysql

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

Related

In phpmyadmin how to add fields to the top instead of the bottom?

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));

How to say or in MySQL statement

I'm trying to match two choices. "One or the other"
SELECT * FROM course WHERE category='blue' || category='red' ORDER BY id DESC LIMIT 5
That is my first thought is to use
||
SQL does support logical operators as well.
By the way, you can shorten this statement to
SELECT * FROM course WHERE category IN ('blue','red') ORDER BY id DESC LIMIT 5
use the word or instead of ||.

Return only one tuple of SQL query result

I want to do a query like
select * from chr2;
but only have MySQL return the first tuple (or an arbitrary) tuple instead of all of them.
How do I do it?
Use the LIMIT clause:
SELECT * FROM chr2 LIMIT 1;
If you want an arbitrary row returned, you have to sort your rows by an random col like this (MySQL docu):
SELECT * FROM chr2
ORDER BY RAND()
LIMIT 1;
On large tables, however, you might run into performance problems with this, as there a random value has to be created for each row and the table has to be sorted according to this column.
Try this ::
select * from chr2 limit 1

rollup with limit?

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)");

Mysql: return orderBy ranking

Is there a way to return the Mysql orderBy ranking as part of the resulting record?
For example, lets say I have a comment table where I am querying results ranked by:
comment_rating and comment_length, using orderBy('r.comment_rating*r.comment_length').
Now I want the resulting records to include the value of their respective comment_rating*comment_length calculations.
Is this possible?
edit: also, does doctrine perform the ranking calculations twice if I do this and also use the same algorithm for orderBy?
Do you mean something like:
SELECT *, (comment_rating * comment_length) AS ranking FROM comment ORDER BY ranking DESC
Edit
Haven't used Doctrine, but after a quick glance at the documentation, I guess it would be something like this:
$q = Doctrine_Query::create()
->select('*, (comment_rating * comment_length) AS ranking')
->from('comment')
->orderBy('ranking');
$comments = $q->execute();
Try this :
Select comment_rating, comment_length,
(comment_rating * comment_length) as rat_len
From comment
OrderBy rat_len
All you need to do is include
(comment_rating*comment_length) as comment_ranking
in the SELECT field list.
SELECT
comment_rating,
comment_length,
comment_rating*comment_length AS comment_rank
FROM
tablename
ORDER BY
comment_rank;
Try this:
SELECT <yourFields>, (r.comment_rating * r.comment_length) AS Rating FROM ...
Documentation:
Doctrine Query Language: Aggregate-values.