mySQL Query WHERE... OR... Shortcut - php

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);

Related

Check if specific value exists in mysql column

I have mysql column called categories. It can contain single or multiple values like this: 1 or 2 or 1,2,3 or 2,12...
I try to get all rows containing value 2.
$query = "SELECT * FROM my_table WHERE categories LIKE '2'";
$rows = mysql_query($query);
This returns row if column only has value 2 but not 1,2,3 or 2,12. How I can get all rows including value 2?
You can use either of the following:
% is a wildcard so it will match 2 or 1,2, etc. Anything on either side of a 2. The problem is it could match 21, 22, etc.
$query = "SELECT * FROM my_table WHERE categories LIKE '%2%'";
Instead you should consider the find_in_set mysql function which expects a comma separated list for the value.
$query = "SELECT * FROM my_table WHERE find_in_set('2', `categories`)";
Like #jitendrapurohut said, you can do it using
$query = "SELECT * FROM my_table WHERE categories LIKE '%2%'";
$rows = mysql_query($query);
But is really bad to store collections like this. A better aproach is as follow:
categories(id_c, name) => A table with each category
my_table(id_m [, ...])
categories_my_table(id_c, id_m)
Then use this query:
SELECT *
FROM my_table m
INNER JOIN categories_my_table cm ON m.id_m = cm.id_m
INNER JOIN categories c ON cm.id_c = c.id_c
WHERE
c.id_c = 2;
EDIT:
#e4c5 link explains why it is bad to store collections like this...
SELECT * FROM my_table WHERE categories LIKE '%2%' AND categories!='1,2,3' AND categories!='2,12';

how to get multiple result from multiple queries

I have this query
$query = "Select * FROM table WHERE table.firs_column = 1;
Select * FROM table WHERE table.second_column = 1;
Select * FROM table WHERE table.third_column = 1;
Select * FROM table WHERE table.column = 1";
$stmt = $db->prepare($query);
$result = $stmt->execute();
I want to have multiple results, each one have the result of one query!
how to do it?
It looks like you are using PDO, so you could do something like:
$first_set = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->nextRowset();
$second_set = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->nextRowset();
$third_set = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->nextRowset();
$fourth_set = $stmt->fetchAll(PDO::FETCH_ASSOC);
To get your 4 rowsets.
You can use UNION if table is same for your multiple queries
Select * FROM table WHERE table.firs_column = 1
UNION
Select * FROM table WHERE table.second_column = 1
UNION
Select * FROM table WHERE table.third_column = 1
UNION
Select * FROM table WHERE table.column = 1
After you finish with the first result set, use nextRowset() method to advance to the next result set.
You need to have a PHP MySQL driver extension that supports this method.
Not sure what you're aiming at, but did you try UNION?
Your SQL statement would look like this:
SELECT * FROM table WHERE table.firs_column = 1
UNION
SELECT * FROM table WHERE table.second_column = 1
UNION
SELECT * FROM table WHERE table.third_column = 1
UNION
SELECT * FROM table WHERE table.column = 1;
Please show your desired result if you think of something different.
Since we don't know anything about your database structure, I suggest looking into mysqli::multi_query().
If you're trying to pull related data, I highly suggest you look into doing MySQL JOINs instead. MySQL is another language unto itself that should be learned as a distinct language rather than just a string to be contatenated in PHP.

How can I select rows only where first digit is a number from 0 to 9?

As far as I know I can do something like:
"SELECT *
FROM my_table
WHERE my_field LIKE '0%'
OR my_field LIKE '1%'
OR my_field LIKE '2%' ";
Is there a way to achieve this with a regular expression or something like this:
"SELECT *
FROM my_table
WHERE my_field LIKE [only first char is 0-9]"??
EDIT: The field is not numeric and it can be something like "1 People", "211 Pies" and so on.
Try this
SELECT *
FROM BadlyDesignedTable
WHERE AnswerColumn RLIKE '^[0-9]+'
I was wondering if it was even possible to regex in where, found it on google in 30 seconds.
SELECT * FROM table WHERE field REGEXP '^[0-9]'
Select * From my_table Where (REGEXP_LIKE(my_field, '[[:digit:]]%'))
The (REGEXP_LIKE(Source_String, '[[:character class:]]')) is a function you can use for numerous issues such as the one you have. Simply use it to tell it to do that for the first digit.
http://docs.oracle.com/cd/B14117_01/server.101/b10759/conditions018.htm
EDIT: Select * From my_table Where (SUBSTR(my_field,1,1) = '[[:digit:]]')
Tried this in a similar query of mine, and it works.
SELECT *
FROM my_table
WHERE left(my_field,1) REGEXP '^[0-9]+';
If you have MSSQL you can use
SELECT *
FROM my_table
WHERE isNumeric(left(my_field,1)) = 1
A simple one without regular expressions:
SELECT *, SUBSTR(my_field, 1, 1) AS mf FROM my_table HAVING mf BETWEEN '0' AND '9';
The quotes in the between clause are important!

Can I use multiple parameters when using WHERE in MYSQL

(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

codeigniter, active record, having problem

I have a problem with Code Igniter having clause.
I need to produce the following SQL with active record:
SELECT *
FROM A
GROUP BY A.X
HAVING A.Y = 'test'
But using the following code:
$this->db->select('*');
$this->db->from('A');
$this->db->group_by('A.X');
$this->db->having('A.Y','frontend');
Produces:
SELECT *
FROM A
GROUP BY A.X
HAVING A.Y = test
And it seems impossible to escape the string value... Or is it?
Write the having clause in a such clumsy way:
$this->db->having('A.Y = "frontend"', null, false);

Categories