(when selecting data from tables in MYSQL database)
something like this:
WHERE some_column = some_value, other_column = other_value
If not, Is there other simple method rather than going into "if loops"
Yes, just separate them with ANDs:
WHERE some_column=some_value AND other_column=other_value
WHERE some_column = some_value AND other_column = other_value
Related
$user_id = '2';
In my database having one column with values like '2,3,5'. I just need to check whether the $user_id value is that column. Is there any easy way to check it?
As #Devsi Odedra, you can use find_in_set as:
SELECT *FROM table_name WHERE FIND_IN_SET($user_id,user_id) <> 0
Please read about find_in_set
Or you can also use like Operator as:
SELECT *FROM table_name WHERE CONCAT(",",user_id, ",") LIKE '%,$user_id,%';
If I have a database with a field called items with '1,2,3,7,15,25,64,346' in it.
Is there a function to use the commas as a separator and take each number and start a new query and get info for that id? Like a foreach but in sql?
I can see it so clear but cannot put it in words...
You can use this query,
SELECT * FROM TABLE_NAME WHERE COLUMN_NAME in (1,2,3,7,15,25,64,346);
Yes, there is. You can use command IN
mysql_query( 'SELECT * FROM `table` WHERE `id` IN ( 1,2,3,4,5 );' );
WHERE - IN
$row = '1,2,3,7,15,25,64,346'; // $row = $field['value']
$arr = explode(',',$row);
foreach($arr as $a) {
// write your query
}
You can use the query SELECT * FROM table_name WHERE id IN ( 1,2,3,4,5 ) to find data having id's.
No need to separate the values. This will give you the desired result.
There is so way to do a "SQL foreach". Do it using PhP
Also, having such fields means your database is not normalized. You should give a look at relational database normalization, otherwise the bigger your database will be, the bigger your problems will be.
It seems you want data like this where 'source' is table having the column
SELECT target.* FROM target
LEFT JOIN source ON FIND_IN_SET(target.id, source.ids_row)
Is there a shortcut for writing the following mySQL query? I am looking for a shorthand to compress
SELECT * FROM `listings` where `bedroom` = 1 OR `bedroom` = 2
because I want to make it easier to dynamically build a mySQL query in PHP. Something like WHERE bedroom = 1, 2 because the numbers that I am getting from PHP is in an array bedroom[1] = 1, bedroom[2] = 1.
SELECT * FROM `listings` where `bedroom` = 1 OR `bedroom` = 2;
And because I am using Codeigniter, some shortcut for this in Active Record will be great too!
I don't understand very well what you're asking; do you need a shorter query?
If so, try this:
SELECT * FROM listings WHERE bedroom IN (1,2)
With ActiveRecord:
$this->db->from("listings")->where_in("bedroom",bedroom)
Use the IN syntax:
SELECT * FROM `listings` where `bedroom` IN (1,2);
You can write:
SELECT * FROM `listings` where `bedroom` in (1,2);
What is the easiest, simplest way to select the max of a column from a table using Zend_Db_Table? Basically, I just want to run this query in Zend:
SELECT MAX(id) AS maxID FROM myTable;
You need to use Zend_Db_Expr to use mysql functions:
return $this->fetchAll(
$this->select()
->from($this, array(new Zend_Db_Expr('max(id) as maxId')))
)
);
You can run direct sql, using $db->query(); yours would simply be:
$db->query("SELECT MAX(id) AS maxID FROM myTable");
but if you want the object notation, then you'd do something like this:
$db->select()->from("myTable", array(new Zend_Db_Expr("MAX(id) AS maxID")));
For those looking to just select the max id from their id column in Zend Framework 2 (maybe 3 as well), but getting this error...
While processing primary key data, a known key id was not found in the data array
...note that you'll need to alias MAX(id) as id.
Example inside a table extended from the TableGateway class:
$select = $this->sql->select();
$select->columns(['id' => new Expression('MAX(id)')]);
$maxId = $this->selectWith($select)->current()->id;
return (int) $maxId;
Another way is like this :
$select=new Zend_Db_Select($db);
$select->from(array($table),array('max($column)'));
$select->where('condition');
$answer=$db->fetchOne($select);
If You do it like this , you can edit it later easier!!!
$select = new Select();
$select->from('tablename');
$select->where(array('post_id', 1));
$select->columns(array('id' => new Expression('MAX(id)')));
$results = $this->getTableGateway()->selectWith($select);
I was wondering how I can get something like this:
SELECT * from `table` WHERE id='1' AND (title='hurr' OR title='durr')
in Zend framework? To my knowledge there are only where() functions, which together make AND relationships and orWhere() which adds an OR rule, but if used together, a select like this:
$select=$this->select()->where("id='1'")->where("title='hurr'")->orWhere("title='durr'");
This would create a query like this
SELECT * from `table` WHERE id='1' AND title='hurr' OR title='durr'
Which is something completely different.
$select = $this->select()->where('id = 1')->where("title='durr' OR title='hurr'");
$select = $this->select()->where('id = 1')->where('title IN (?)', array('durr', 'hurr'));
You can do something like
$select=$this->select()->where("id='1'")->where("title='hurr' OR title='durr'");
You can check the manual