In my sql table i had more then 300000 entries.
$marketname = more then 200 !
with this select i can see the last 15 entries.
$sql = "SELECT price FROM markets where market = '" . $marketname . "' order by time desc LIMIT 15,1";
$sql = "SELECT price FROM markets where market = '" . $marketname . "' order by time desc LIMIT 15,1";
and so on for the last 30, 60, 120.
$sql = "SELECT price FROM markets where market = '" . $marketname . "' order by time desc LIMIT 30,1";
$sql = "SELECT price FROM markets where market = '" . $marketname . "' order by time desc LIMIT 60,1";
But is there a combination of all, because when i show it on one page its very very slow with 200 markets!!
like..
$sql = "SELECT price FROM markets where market = '" . $marketname . "' order by time desc LIMIT 15,1 AND LIMIT 30,1 AND LIMIT 60,1 AND LIMIT 120,1";
First of all, try to not do SQL injection style SQL. Use PDO or MySQLi with prepared statements. With that in mind, do not limit your query's last part.
SELECT price FROM markets WHERE market = :marketName ORDER BY time DESC limit 100
Would query the price of the market by marketname (prep statement), order it and limit it to 100 records.
Related
I am new in CodeIgniter, I want to count all rows from database table but i use limit in query and i want all count without use limit how can i do ?
my code is below :
$sql = " SELECT intGlCode,fkCategoryGlCode,'C' as acctyp,varEmail,varContactNo as phone,CONCAT(varFirstName,' ',varLastName) as name,dtCreateDate,chrStatus,varMessage as message
FROM " . DB_PREFIX . "Customer WHERE varEmail='$userEmail'
UNION
SELECT intGlCode,'' as fkCategoryGlCode,'P' as acctyp,varEmail,varPhoneNo as phone,varName as name,dtCreateDate,chrStatus,txtDescription as message FROM
" . DB_PREFIX . "Power WHERE varEmail='$userEmail' ORDER BY intGlCode DESC
LIMIT $start, $per_page ";
$query = $this->db->query($sql)
i use limit for pagination but i want to get all record from table.
You can add new column in both above and below UNION queries. It will be like below.
select (select count(*) from your_query), your_columns from query_above_union
UNION
select (select count(*) from your_query), your_columns from query_below_union
your_query = your full actual query your are using currently.
Although I am not sure about Codeigniter. But sure about SQl.
* If you count all records with all data including limit, than you can use this code. please check it. I hope it will works for you.*
$countsql = " SELECT intGlCode,fkCategoryGlCode,'C' as acctyp,varEmail,varContactNo as phone,CONCAT(varFirstName,' ',varLastName) as name,dtCreateDate,chrStatus,varMessage as message
FROM " . DB_PREFIX . "Customer WHERE varEmail='$userEmail'
UNION
SELECT intGlCode,'' as fkCategoryGlCode,'P' as acctyp,varEmail,varPhoneNo as phone,varName as name,dtCreateDate,chrStatus,txtDescription as message FROM
" . DB_PREFIX . "Power WHERE varEmail='$userEmail' ORDER BY intGlCode DESC";
$sql = $countsql. " LIMIT $start, $per_page";
$totalRecords = $this->db->query($countsql);
$result["total_rows"] = $totalRecords->num_rows();
$query = $this->db->query($sql);
$result["list"] = $query->result_array();
I have a query for selecting random records with a limit of 6.
$query = $this->pdo->prepare("SELECT * FROM `" . $this->table . "` ORDER BY rand() LIMIT " . $limit);
If I set the limit to 6, it will sometimes only show 4 records, sometimes 5.
How can I make it always show 6?
I have more than 6 records in the database.
I looked at some questions around this here but couldn't find a clear answer.
$query = $this->pdo->prepare("SELECT * FROM `" . $this->table . "` ORDER BY rand() LIMIT $limit " );
Try with :
SELECT RAND(6)
$query = $this->pdo->prepare("SELECT * FROM " . $this->table . " ORDER BY RAND(6) LIMIT " . $limit);
I am trying to create a query using multiple WHERE conditions, both AND and NOT, and I am having trouble. Both conditions work separately, but not together. Most likely I've gotten the syntax wrong.
Here's the query
$getposts = mysql_query("SELECT * FROM Posts WHERE (category=".$_POST['category'].") AND (id NOT IN ( '" . implode($array, "', '") . "' )) ORDER BY popularity DESC") or die(mysql_query());
I've tried with and without parentheses, but with no change. As I say, both this query:
$getposts = mysql_query("SELECT * FROM Posts WHERE category=".$_POST['category']." ORDER BY popularity DESC") or die(mysql_query());
and this query:
$getposts = mysql_query("SELECT * FROM Posts WHERE id NOT IN ( '" . implode($array, "', '") . "' ) ORDER BY popularity DESC") or die(mysql_query());
work, just not when combined.
Any help is much appreciated!
I'm making a feed wall, and two services save posts to the same database table.
One posting service is used way more than the other, so on the wall I want to limit each service to the 25 newest posts (total 50) on the front page for equal representation.
This is what I originally had, without evening the posts:
$sql = "SELECT * FROM posts";
$sql .= " WHERE disq = 0";
$sql .= " AND approved = 1";
$sql .= " ORDER BY created_at DESC";
$sql .= " LIMIT 50";
but then I try to limit them by service:
$sql_1 = "SELECT * FROM posts";
$sql_1 .= " WHERE disq = 0";
$sql_1 .= " AND approved = 1";
$sql_1 .= " AND source = 'TW'";
$sql_1 .= " ORDER BY created_at DESC";
$sql_1 .= " LIMIT 25";
$sql_2 = "SELECT * FROM posts";
$sql_2 .= " WHERE disq = 0";
$sql_2 .= " AND approved = 1";
$sql_2 .= " AND source = 'IG'";
$sql_2 .= " ORDER BY created_at DESC";
$sql_2 .= " LIMIT 25";
Doing something like
$sql = $sql_1 UNION $sql_2;
Doesn't seem to work, because all the examples I see perform LIMIT at the end of a bunch of queries. And an ORDER BY should be performed after that, to reorder the posts chronologically and make the wall mixed service.
MySQL help is appreciated
You could omit your ORDER BY in your subqueries and just do it at the end, after the union.
(
SELECT * FROM POSTS
WHERE disq = 0 AND approved = 1 AND source= 'TW'
LIMIT 25
)
UNION ALL
(
SELECT * FROM POSTS
WHERE disq = 0 AND approved = 1 AND source= 'IG'
LIMIT 25
)
ORDER BY created_at DESC
More info on the union and how to sort is explained at this MySQL documentation
You need to pull the most recent 25 items separately from your two criteria sets, then put them together and order them again.
That will go like this. You need these parenthetical subqueries because the ORDER BY ... LIMIT clauses need to be associated with each one separately.
SELECT *
FROM
( SELECT *
FROM POSTS
WHERE disq = 1 AND approved = 1 AND source= 'TW'
ORDER BY created_at DESC
LIMIT 25
) A
UNION ALL
( SELECT *
FROM POSTS
WHERE disq = 1 AND approved = 1 AND source= 'IG'
ORDER BY created_at DESC
LIMIT 25
) A
ORDER BY created_at DESC, source
An index on your POSTS table on (disq, approved, source, created_at) will serve to speed up this query.
I need to have my results sorted by "ORDER BY prod_name" in my SQL statement but I cannot figure out get it to work. I tried after
$thisProduct .= " AND prod_type = 1 ORDER BY prod_name";
and also after
$thisProduct .= " AND ID = '" . mysql_real_escape_string($_GET['product']) . "' ORDER BY prod_name";
But I cannot get my results to sort correctly. Am I placing the order by in the wrong spot or did I query the DB incorrectly?
Thank you in Advance, I am still pretty new at MYSQL queries.
$thisProduct = "SELECT prod_name AS Name, days_span, CONCAT(LEFT(prodID,2),ID) AS ID, geo_targeting FROM products WHERE status = 'Active' AND vendID = ".$resort['vendID'];
if (isset($_GET['product']) AND is_numeric($_GET['product'])) {
$thisProduct .= " AND ID = '" . mysql_real_escape_string($_GET['product']) . "'";
}
else {
$thisProduct .= " AND prod_type = 1";
}
$thisProduct .= " LIMIT 1";
$getThisProduct = mysql_query($thisProduct);
if (!$getThisProduct/* OR mysql_num_rows($getThisProduct) == 0 */) {
header("HTTP/1.0 404 Not Found");
require APP_PATH . '/404.html';
die();
}
$thisProductData = mysql_fetch_assoc($getThisProduct);
You should have:
$thisProduct .= " ORDER BY prod_name";
$thisProduct .= " LIMIT 1";
(Note that the LIMIT 1 means you only get one record).
Assuming that your query is correct and you want the first product by name:
$thisProduct .= " ORDER BY prod_name LIMIT 1";
I believe it should go right before your "LIMIT 1", as in:
$thisProduct .= " ORDER BY prod_name LIMIT 1";
Insert it before the LIMIT
$thisProduct .= " ORDER BY prod_name LIMIT 1";
You can the select syntax at http://dev.mysql.com/doc/refman/5.0/en/select.html
SELECT query usually takes following form
SELECT which_all_to_select
FROM which_table/tables
WHERE criteria
ORDER BY column_name ASC/DESC;
ASC ascending order, and DESC is descending order
This orders query results by column_name specified in ORDER BY clause .