I have a table which has many columns but there are two main columns called District and State. My sql query is as follows.
$query = "SELECT * FROM table WHERE District = '" . $var . "' ORDER BY Date DESC";
this query returns me data from the district column.
Now this is what I want.In some rows the District column is empty, so in that case I want the query to lift data from State column. Is that possible?
Can that be done in mysql query or will I have to write something in php?
what do you mean by empty? If it is NULL then you should use COALESCE
SELECT *, COALESCE(District, State) NewDistrict
FROM...
but if it is empty as ''. then
SELECT *, IF(District = '', State, District) NewDistrict
FROM...
SELECT *
FROM table
WHERE District = '$var'
OR (District='' AND State='var')
ORDER BY Date DESC
$query = "SELECT * FROM table WHERE District = '" . $var . "' OR State = '" . $var . "' ORDER BY Date DESC";
Related
Content of entire pos column is home01+home03 or home02+home04.
I need to select rows where pos contains home01.
$pos = 'home01';
$stmt = $db->query("select * from banners where pos contains '" . $pos . "'");
Nothing is selected.
Also I need to avoid LIKE statement because of large table.
Any help?
You can use match againts
ALTER TABLE table_name ADD FULLTEXT(pos);
SELECT * FROM banners MATCH(pos) AGAINST('+$pos+');
use this query
select * from banners where pos like'%" . $pos . "'";
this query return all row where first match home01
I am trying to filter users using country wise . $country is an array containing the name of countries. I want to run the query in such a way that if there is nothing in country then the query gives me all the rows. how can i acheive this.
I am doing this :
"SELECT * from users where country IN('".implode("','",$country)."')";
thanks in advance
$sql = "SELECT * FROM users";
if($country) {
$sql .= " WHERE country IN('" . implode("','", $country) . "')";
}
For each filter item just check if it is NULL (i.e. don't filter the results based on this) or if it is equal/in:
SELECT *
FROM users
WHERE ('".implode("','",$country)."' IS NULL OR country IN('".implode("','",$country)."'))
AND ($forename IS NULL OR forename = $forename)
I have a session that contains an array and that array is filled with id's. What is the best way to select all the rows from a MySQL table that correspond to these id's?
So I need something like:
SELECT * FROM table WHERE id = $_SESSION['ids']
Obviously, this doesn't work, since $_SESSION['ids'] is an array.
SELECT *
FROM
table
WHERE
find_in_set(id, $_SESSION['ids'])>0
You can just use IN SQL operator.
In this case your query will look like
$sql = 'SELECT * FROM table WHERE id IN ("' . implode('","', $_SESSION['ids'] . '")';
This code will produse a query like following:
SELECT * FROM table WHERE id IN ("1", "2", "foo");
Hope it helps
HI Try This,
$var = $_SESSION['ids'];
and then fire your query as
$sql = SELECT * FROM table WHERE id = '$var';
I have a table (country_table) with a list of all countries and their respective ids.
|country_id|country_name|
|ES |Spain |
.
.
.
I have an array, fetched from another table using fetch_array that looks like this:
$array = Array(
[0]=>Array([country_id]=>'ES')
[1]=>Array([country_id]=>'DE')
[2]=>Array([country_id]=>'GB'))
How can I select the records (country_id and country_name) from country_table that do not exist in the table but exist in the array?
$sql ="SELECT country_id, country_name FROM country_table
WHERE country_id NOT IN (". implode(",", $array) .")";
implode() function will generate comma-seprated string representation of array elements .
This is the sql.
select country_id, country_name from table where country_id not in ('ES','DE','GB');
// gather unwanted id's first
$notInIds = array();
foreach ($array as $arr) $notInIds[] = $arr['country_id'];
// prepare query
$query = 'SELECT `country_id`, `country_name` FROM `your_table`
WHERE `country_id` NOT IN (' . implode(',',$notInIds) . ')';
// then execute it
For the question: "How can I select the records (country_id and country_name) from country_table that do not exist in the table but exist in the array?"
I guess you have to write some code (eg. in PHP) in order to achieve the result you want.
The problem is that you can not display data that is not in the table (eg. country_name), that's not possible as you don't know this data. The only possibility would to show the country_id's which are in the array and not in the table as this is the only data you have...
.Hi guys, i need a little help with a project that i'm currently working on.
I have this database where i have a table containing records of students from different departments namely:
Dept1
Dept2
Dept3
Dept4
Dept5
in my batch.php i have two dropdown menus for Batch(which is the name of the tables) and Department(a field used for sorting).
what i want to do is that when the user chooses a Batch and a Department and then clicks the submit button, it will automatically assign the values chosen to a MySQL query for the SELECT method. the SELECT query should now retrieve all records from the table sort it by department but will display first the Department that was chosen above.
.Any help would be much appreciated. Thanks in advance and more power!
You can sort by a boolean expression, which will return 1 when true and 0 when false.
<?php
$batch = mysql_real_escape_string($_POST['batch']);
$department = mysql_real_escape_string($_POST['department']);
$query = "SELECT * FROM `" . $batch . "`" .
" WHERE department = '" . $department . "'" .
" ORDER BY department = '" . $department . "' DESC, department;
?>
Update: You should validate the $batch argument that it is a valid table expected, the code as is is vulnerable to SQL injection.