$test2=mysql_query("SELECT min(substr(status,1) FROM railways");
while($test_array1=mysql_fetch_array($test2)){
echo "<pre>";
print_r($test_array1);
echo "</pre>";
}
what is the correct form of this query...need to get the min value from the table itself..
substr(status, 1) returns the string as it is ... mysql counts in strings from 1, not from 0.
if i got you right using substr(status,2) is what you are looking for.
best regards
phil
substr(status, 1) is going to give 'w21', but substr(status, 2) is going to give 21. If you want to get the minimum and the pattern is DIGIT_NUMBER_NUMBER, you will use min(substr(status, 2)). From this data set: w21, c33, d55, d11, it will pull the minimum number you want.
Related
I used the following statement to filter records by subject ids. (Here subject_id=5)
$this->datatables->where('letter_letter.subject_id', 5);
That is working perfectly. Further I want to filter records in subject_id range like 1 to 10. Then I changed my code as follows :
$this->datatables->where('letter_letter.subject', 10, '<');
But did not get the desired output. How can I edit my code to get expected result ? Can anyone help me ?
Just use two calls to where() to define the range:
$this->datatables->where('letter_letter.subject_id >= ', 1);
$this->datatables->where('letter_letter.subject_id <=', 10);
This is one way you can make a where query with two conditions:
$this->datatables->where("letter_letter.subject_id IS ? AND letter_letter.subject BETWEEN ? AND ?", 5, 1, 10)
I've been trying to find a solution for my query for some time now. But i haven't been able to solve it yet. Most of it works nicely, but the count part does not work like i intend it to.
My query looks like this:
$years = Sample::whereCostumerId($id)
->groupBy('year')
->orderBy('year', 'DESC')
->get(array(
DB::raw('year'),
DB::raw('COUNT(id) as antalProver'),
DB::raw('MIN(provnr) AS minProvnr'),
DB::raw('MAX(provnr) AS maxProvnr'),
DB::raw('count(P_HCl) AS numP_HCl'),
DB::raw('count(Total_lerhalt) AS numLerhalt'),
DB::raw('ROUND(AVG(pH),1) AS avgpH'),
DB::raw('ROUND(AVG(P_AL),1) AS avgP_AL'),
DB::raw('ROUND(AVG(K_AL),1) AS avgK_AL'),
DB::raw('AVG(X) AS coordExist')
));
The issue here is that many of the rows in the column P_HCl and Total_lerhalt contains zero. And i don't want to count these. I only want to count where value is greater than zero.
Im shure there's some nice solution for this.
If you guys have any other solution for the query all together id be happy to see it.
Thanks
You‘re most of the way there—you just need to add a where clause to your query:
$years = Sample::whereCostumerId($id)
->groupBy('year')
->orderBy('year', 'DESC')
->where('year', '>', 0)
->get(array(
DB::raw('year'),
DB::raw('COUNT(id) as antalProver'),
DB::raw('MIN(provnr) AS minProvnr'),
DB::raw('MAX(provnr) AS maxProvnr'),
DB::raw('count(P_HCl) AS numP_HCl'),
DB::raw('count(Total_lerhalt) AS numLerhalt'),
DB::raw('ROUND(AVG(pH),1) AS avgpH'),
DB::raw('ROUND(AVG(P_AL),1) AS avgP_AL'),
DB::raw('ROUND(AVG(K_AL),1) AS avgK_AL'),
DB::raw('AVG(X) AS coordExist')
));
I have written a SQL query, which extracts the lowest weekly price for a property stored in my database:
var rPropertyId = Request.QueryString["PropertyID"];
var cheapestrate = "SELECT TOP 1 * FROM RateInfo WHERE PropertyID=#0 ORDER BY RateWeekly ASC";
var qcheapestrate = db.QuerySingle (cheapestrate, rPropertyId);
I'm pretty confident that this statement is correct. The problem i have, is that not ALL propertys have pricing, so i only want to show this price if they do. I have created the below if statement, but it's telling me i'm missing an ; somewhere?
#if(qcheapestrate=!null){
Rates From qcheapestrate.rateweekly per week
}
So i'm trying to check if the query returns an entry. if it does, i want to show the lowest "rateweekly" value. Hopefully this all makes sense!
Try this...
#if(qcheapestrate!=null){
<text>Rates From</text>
#qcheapestrate.rateweekly
<text>per week</text>
}
I have two tables, one is static database that i need to search in, the other is dynamic that i will be using to search the first database. Right now i have two separate queries. First on page load, values from second table are passed to first one as search term, and i am "capturing" the search result using cURL. This is very inefficient and probably really wrong way to do it, so i need help in fixing this issue. Currently page (html, front-end) takes 40 seconds to load.
Possible solutions: Turn it into function, but still makes so many calls out. Load table into memory and then run queries and unload cache once done. Use regexp to help speed up query? Possible join? But i am a noob so i can only imagine...
Search script:
require 'mysqlconnect.php';
$id = NULL;
if(isset($_GET['n'])) { $id = mysql_real_escape_string($_GET['n']); }
if(isset($_POST['n'])) { $id = mysql_real_escape_string($_POST['n']); }
if(!empty($id)){
$getdata = "SELECT id, first_name, last_name, published_name,
department, telephone FROM $table WHERE id = '$id' LIMIT 1";
$result = mysql_query($getdata) or die(mysql_error());
$num_rows = mysql_num_rows($result);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo <<<PRINTALL
{$row[id]}~~::~~{$row[first_name]}~~::~~{$row[last_name]}~~::~~{$row[p_name]}~~::~~{$row[dept]}~~::~~{$row[ph]}
PRINTALL;
}
}
HTML Page Script:
require 'mysqlconnect.php';
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$getdata = "SELECT * FROM $table WHERE $table.mid != '1'ORDER BY $table.$sortbyme $o LIMIT $offset, $rowsPerPage";
$result = mysql_query($getdata) or die(mysql_error());
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$idurl = 'http://mydomain.com/dir/file.php?n='.$row['id'].'';
$p_arr = explode('~~::~~',get_data($idurl));
$p_str = implode(' ',$p_arr);
//Use p_srt and p_arr if exists, otherwise just output rest of the
//html code with second table values
}
As you can see, second table may or may not have valid id, hence no results but second table is quiet large, and all in all, i am reading and outputting 15k+ table cells. And as you can probably see from the code, i have tried paging but that solution doesn't fit my needs. I have to have all of the data on client side in single html page. So please advice.
Thanks!
EDIT
First table:
id_row id first_name last_name dept telephone
1 aaa12345 joe smith ANS 800 555 5555
2 bbb67890 sarah brown ITL 800 848 8848
Second_table:
id_row type model har status id date
1 ATX Hybrion 88-85-5d-id-ss y aaa12345 2011/08/12
2 BTX Savin none n aaa12345 2010/04/05
3 Full Hp 44-55-sd-qw-54 y ashley a 2011/07/25
4 ATX Delin none _ smith bon 2011/04/05
So the second table is the one that gets read and displayed, first is read and info displayed if ID is positive match. ID is only unique in the first one, second one has multi format input so it could or could not be ID as well as could be duplicate ID. Hope this gives better understanding of what i need. Thanks again!
A few things:
Curl is completely unnecessary here.
Order by will slow down your queries considerably.
I'd throw in an if is_numeric check on the ID.
Why are you using while and mysql_num_rows when you're limiting to 1 in the query?
Where are $table and these other things being set?
There is code missing.
If you give us the data structure for the two tables in question we can help you with the queries, but the way you have this set up now, I'm surprised its even working at all.
What you're doing is, for each row in $table where mid!=1 you're executing a curl call to a 2nd page which takes the ID and queries again. This is really really bad, and much more convoluted than it needs to be. Lets see your table structures.
Basically you can do:
select first_name, last_name, published_name, department, telephone FROM $table1, $table2 WHERE $table1.id = $table2.id and $table2.mid != 1;
Get rid of the curl, get rid of the exploding/imploding.
I'm trying to split an HTML document into its head and body:
my #contentsArray = split( /<\/head>/is, $fileContents, 1);
if( scalar #contentsArray == 2 ){
$bodyContents = $dbh->quote(trim($contentsArray[1]));
$headContents = $dbh->quote(trim($contentsArray[0]) . "</head>");
}
is what i have. $fileContents contains the HTML code. When I run this, it doesn't split. Any one know why?
The third parameter to split is how many results to produce, so if you want to apply the expression only once, you would pass 2.
Note that this does actually limit the number of times the pattern is used to split the string (to one fewer than the number passed), not just limit the number of results returned, so this:
print join ":", split /,/, "a,b,c", 2;
outputs:
a:b,c
not:
a:b
sorry, figured it out. Thought the 1 was how many times it would find the expression not limit the results. Changed to 2 and works.