Using multiple SELECT statements to narrow results - php

I have a database with some properties that have been sold. They each have a date associated with it which is what I need to use to narrow the search results.
Basically I have a query:
$query = "SELECT * from newsales WHERE city = '".$_GET['location']."'";
And what I need to do is, from the results returned from the above query, I need to further narrow it down to be within the past 90 days.
So it'll find the city, and then it needs to ONLY get the ones from the last 90 days. How do I combine SELECT statements to narrow the results down?
Thanks!

You could use the SQL AND operator.
Doc: http://dev.mysql.com/doc/refman/5.1/en/logical-operators.html
Your query would be than:
$query = "SELECT * from newsales WHERE city = '".$_GET['location']."' AND date > '".$oldestdate."';
Set $date to the oldes date, in your case 90 days before today. According to your dateformat in your mysql database you have to calculate this in a timestamp or datetime.

You don't need to combine SELECT statements, just make a more complex WHERE clause using the boolean AND operator:
$query = "
SELECT *
FROM newsales
WHERE
city = '".$_GET['location']."'
AND date > '".$oldestdate."'
";
I'd advise you to read up on SQL injection as well - if you use $_GET directly like that, someone can come to your website and basically type in any SQL statement they want.
The easiest way, assuming you are using mysqli_* functions (which have replaced the mysql_* functions but can be used mostly interchangeably) is mysqli_real_escape_string(), e.g. city = '" . mysqli_real_escape_string($_GET['location']) . "'.

Combining results of multiple SELECT statements means you need to use UNION.
Please see this : http://dev.mysql.com/doc/refman/5.0/en/union.html
(SELECT a FROM t1 WHERE a=10 AND B=1 ORDER BY a LIMIT 10)
UNION
(SELECT a FROM t2 WHERE a=11 AND B=2 ORDER BY a LIMIT 10);

Related

How to get results from multiple tables using a single query in CodeIgniter?

I have a situation where i need to get data from 7 different tables for a particular processing to be performed.
I will need 7 simple SELECT all statements, nothing fancy. But to minimize the database hit, I will very much like to bundle these queries into 1 or 2 queries.
Like:
select * from table1; select * from table2; select * from table3;.
And will call this query from my code. Is is possible to get the results in something on the similar lines of .net's DataSet in PHP. I am looking for a solution in core PHP or CodeIgniter. I am using PDO for database connection.
PS: The tables have different schemas, no common point. So any solution with join or union will not work.
$results = $this->db-query("select * from tb1; select * from tb2");
now $result[0] should have all the records from tb1 and $results[1] should have the records from tb2.
Something on the similar line will be most helpful in this scenario.
Look into InnerJoin. That's how you make multiple selections at once - assuming you have a common data point
What you need is UNION
$this->db->query('SELECT column_name(s) FROM table_name1 UNION ALL SELECT column_name(s) FROM table_name2');
UNION ALL This is the UNION keyword, and the optional ALL keyword. UNION indicates that the results of the SELECT statement that precedes UNION will be combined with the results of the SELECT statement that follows UNION.
When you use the ALL keyword, duplicate rows are not removed from the combined set that is produced by the union. This can dramatically improve the performance of the query, because Access does not have to check the results for duplicate rows. You should use the ALL keyword if any of the following conditions is true:
You are certain that the select queries will not produce any
duplicate rows.
It does not matter if your results have duplicate rows.
You want to see duplicate rows.
EDITED
After your edition and what I've got it from your question then this might be helpful. You need to loop that queries to get an array of results as
$tablename = array('name1','name2','name3','name4','name5','name6','name7');
$results = array();
foreach($tablename as $key => $value){
$results[$key] = $this->db->query("select * from ".$value."")->result_array();
}
Multiple select query in codigniter
$this->db->select('t1.*, t2.*');
$this->db->from('table1 AS t1, table2 AS t2');
$query = $this->db->get();
$row = $query->result_array();
print_r($row);

Change order of records from MySQL in PHP

How can I change the order by time either in MySQL or in the While Loop?
I have a query like
`"SELECT `start-time` from `table_name`"`
and my start-time will be in format 02:00pm, 02:00am, 05:00am, in likewise. If that's just number without am/pm I can use order by.
Or, if the above way not possible, can I use PHP to order the start time in while loop?
I am using the following way to retrieve the data
$doquery = mysql_query("SELECT `start-time` FROM `table_name`");
while($fetch_query = mysql_fetch_array($doquery)){
echo $fetch_query['start-time'];
}
Thanks
Are you storing your times as TIMESTAMP in MySQL? If not, you should be. The most efficient way to sort the results is going to be by adding an ORDER BY clause to your SQL statement. For instance, to sort so that the most recent times occur first in the result set, try this:
SELECT `start-time` FROM `table_name` ORDER BY `start-time` DESC;
To order so that the most recent times occur last in the result set, change the query to:
SELECT `start-time` FROM `table_name` ORDER BY `start-time` ASC;
The other thing that I would encourage you to explore is PDO. All of the mysql_* PHP functions are deprecated. Prepared queries are a lot more secure and flexible. There are a lot of tutorials out there... this one looks alright: http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html Anyway hope that helps. Happy coding.
If it is simply a string you could use STR_TO_DATE or CASE WHEN, like:
Using STR_TO_DATE
SELECT `start-time` from `table_name`
ORDER BY STR_TO_DATE(start-time,'%h.%i%p')
Using CASE
SELECT `start-time` from `table_name`
ORDER BY
CASE WHEN start-time LIKE '%am%'
THEN 0
ELSE 1
END, start-time
Bu I agree with others you could probably store this as TIMESTAMP and use mySQLi or PDO for your database handling in php.

getting data from table where one field is maximum in one query

i want to get data from my database where one field is max, at this moment i do this in 2 queries. the thing is i dont want to overload the server so i am looking for a way to make it in 1 query. any suggestions?
as you can see i am looking for entry where the timestamp is max.
$query = "SELECT MAX(TIMESTAMP) AS timestamp FROM `data`";
$run_query = mysql_query($query);
$highest = mysql_result($run_query,'0','timestamp');
$query = "SELECT * FROM `data` where `timestamp`='$highest'";
$run_query = mysql_query($query);
thanks in advance.
An alternative, if you can guarantee that there will never be two records with the same timestamp:
SELECT *
FROM data
ORDER BY timestamp DESC
LIMIT 1
If you can have duplicate timestamps, then the other answers with the sub-select are the better solution.
This will simply work as you desired.
SELECT *
FROM data
WHERE timestamp = (SELECT MAX(timestamp) FROM data)
Backticks on this case are optionals. But actually timestamp is a reserved keyword but is permitted to be used even without escaping it with backtick.
SELECT * FROM `data` WHERE `timestamp` = (SELECT MAX(`timestamp`) FROM `data`)
If your goal is to minimize server resources, the difference between one query and two queries is really minor. The engine needs to do pretty much the same work. The difference would be the slight overhead of compiling two queries rather than one.
Regardless of the solution, you will minimize server resources by building an index on data(timestamp).

SQL join or PHP loop? or what?

So i have two queries to give me the information i need and im trying to figure out the best way to get the result from them. i have a table of Holdings. using:
SELECT symbol, sum(shares) AS shares, sum(shares * price) AS cost
FROM Transactions
WHERE (action <>5)
AND date <= '2010-10-30'
GROUP BY symbol HAVING sum(shares) > 0
which results in
AGNC 50.00 1390.0000
RSO 1517.00 9981.8600
T 265.00 6668.7500
I then have another query
SELECT close FROM History WHERE symbol = $symbol AND date = $date
which will return the closing price of that day.
T 24.40
i want to basically for a given date calculate value so sum(shares * close) for each symbol. but i dont know how to do that with out looping through each row in php. i was hoping there was a join or something i could do in sql to make the data return the way i want it
I think you could do something similar to this: A select query selecting a select statement Put your second query literally in your first query. Not sure about exact syntax, but like:
SELECT
symbol,
sum(shares) AS shares,
sum(shares * price) AS cost,
sum(shares * close) as value
FROM Transactions
INNER JOIN History ON (symbol = $symbol AND date = $date)
WHERE (action <>5)
AND date <= '2010-10-30'
GROUP BY symbol HAVING sum(shares) > 0
#popnoodles is right about your naming though. If you use date I'd think you'd need [date].

MySQL select between two dates not working as expected

I'm trying to create a query that will select all dates between two dates
This is my query:
$query = "SELECT DISTINCT * FROM D1,D2
WHERE D1.DATE_ADDED BETWEEN '$date1' AND '$date2' AND D1.D1_ID = D2.D2_ID";
The trouble is, it is not returning anything, but not producing an error either
So I tried inputting it directly into phpMyAdmin like this
SELECT DISTINCT * FROM D1,D2
WHERE D1.DATE_ADDED BETWEEN '2011-01-01' AND '2011-12-12'
AND D1.D1_ID = D2.D2_ID`
then like this
SELECT DISTINCT * FROM D1,D2
WHERE D1.DATE_ADDED BETWEEN '2011-01-01' AND '2011-12-12'
and like this
SELECT * FROM D1
WHERE DATE_ADDED BETWEEN '2011-01-01' AND '2011-12-12'
and I just get
MySQL returned an empty result set (i.e. zero rows). ( Query took 0.0003 sec )
Yes, my tables exist, and so do the columns :)
In the first cases the lack of results could be because of the inner join. For a result to be in the set it would require a record in both tables, ie. a record from d1 would not appear unless d2 also had that id in the d2_id column. To resolve this, if that is correct for your business logic, use left join.
However, the last of your cases (without the join) suggests the reasons is a lack of matching records in the first (left) table d1.
Without the full dataset we can't really comment further, since all the code you are running is perfectly valid.
If you always want to select an entire year it is easer to select it like this:
SELECT * FROM D1 WHERE YEAR(DATE_ADDED) = 2011;
Please implement below code
SELECT DISTINCT * FROM D1,D2
WHERE D1.DATE_ADDED BETWEEN DATE_FORMAT('2011-01-01','%Y-%m-%d')
AND DATE_FORMAT('2011-12-12','%Y-%m-%d')
AND D1.D1_ID = D2.D2_ID`

Categories