select rows containing a string in a specific column - php

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

Related

Sum 3 columns in row for a total column - MySQL & PHP

I have a database which has many columns, but 3 of them I need to sum up. a, b & c .... I need to add a total column for every row (40+ rows) so each time a new score is entered into a,b or c ... the total column is updated for that particular row.
At the moment, I have the following Update that runs to enter a new score.
mysql_query("UPDATE national_reqs SET " . $phase . " = '" .$score . "' WHERE dog_name ='" . $dog_name . "'");
Then when I pull the data back out, I use the following query (and I want to sort by a new "Total" column)
$result = mysql_query("SELECT regfor,cat_num, ipolevel,handler,dog_name,a,b,c,score_time FROM 2015_national_reqs WHERE (id<=190) ORDER BY cat_num ASC") or die(mysql_error());
I think I need a trigger to make this work properly ? I can create a new column for "Total" of type int.
Thoughts ?
update [table] SET a = [value], d = (a+b+c) where id = [id];
Cheers

need rows with specific column entries in MySQL

I need to get the rows which has a specific column value .
is this is correct for it ??
SELECT *
FROM
tourDB
WHERE
tour_type = insta_deals
in this i want to get all the rows having insta_deals in the column of 'tour_type'.
You are missing " "
SELECT *
FROM
tourDB
WHERE
tour_type = "insta_deals"
^^^ ^^^
SELECT * FROM tourDB WHERE tour_type LIKE 'insta_deals'
Without using ' the value insta_deals is understood by mysql as a column name.
So your need to use ' to specify to mysql that this value is actualy a string.
SELECT *
FROM
tourDB
WHERE
tour_type = 'insta_deals'

mysql query to assign value to the same variable from different columns

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

WHERE IN SQL condition problem

I have this query:
"SELECT * FROM informations WHERE ". $id ." IN (ids)"
It only works if $id is the first value from ids... in ids values are "1,2,3,4,5".
Is there a way for it to work with the rest of the ids?
Would this work for you?
"SELECT *
FROM Informations
WHERE ids LIKE \"" . $id . ", %\" -- try to match against the first value in ids
OR ids LIKE \"%," . $id . ",%\" -- try to match against a value in ids that is neither the first nor the last value
OR ids LIKE \"%," .$id . "\" -- try to match against the last value found in ids"
If ids is a field containing comma-delimited values, then your query is like:
SELECT * FROM `informations` WHERE 3 IN ("1,2,3,4,5")
Instead of what it should be:
SELECT * FROM `informations` WHERE 3 IN (1,2,3,4,5)
There is no automatic tokenisation (splitting on ,) performed; the one value of ids is not automatically converted into a list for you such that IN can work.
Unfortunately your table design has been your undoing here. Can you split the IDs into a separate table using the principle of database normalisation?
Then your query might look like:
SELECT * FROM `informations` WHERE 3 IN (
SELECT `id`
FROM `ids`
WHERE `informations`.`id` = `ids`.`information_id`
)
BTW, "information" is a non-countable noun and, as such, "informations" is wrong.
Update (thanks for the idea, a1ex07!)
Although this is hackery and I still suggest fixing your table layout, I'll be kind and suggest a quick fix.
Willempie was close with:
$query = 'SELECT *
FROM `informations`
WHERE `ids` LIKE "%' . $id . '%"';
Unfortunately, a wildcard match isn't quite powerful enough. Consider if ids is like "1,6,9,12,35,4" and $id is like 3. You get a false positive. The LIKE statement needs to be aware of the commas.
You can add multiple cases:
$query = 'SELECT *
FROM `informations`
WHERE `ids` LIKE "%,' . $id . ',%"
OR `ids` LIKE "%,' . $id . '"
OR `ids` LIKE "' . $id . ',%"';
Or, for brevity, you can work around this with regular expressions:
$query = 'SELECT *
FROM `informations`
WHERE `ids` REGEXP "(^|,)' . $id . '(,|$)"';
For any $id you wish to find, before it must be the start of ids (^) or a comma; after it must be a comma or the end of ids ($). This ensures that $id must be found as a whole, comma-delimited token.
It's a little like "Whole Word Only" in word processor searches, but with commas separating "words" instead of spaces.
Update 2
Another way uses FIND_IN_SET, which performs a search within a comma-delimited string:
$query = 'SELECT *
FROM `informations`
WHERE FIND_IN_SET("' . $id . '", `ids`)';
Your query is technically correct but the values for 'ids' are not.
You should enclose the values of ids within single quotes. If I were to write the code without using ids, it would be like this:
"SELECT * FROM informations WHERE ". $id ." IN ('1','2','3','4','5')"
More info on this rule here: http://dev.mysql.com/doc/refman/5.1/en/comparison-operators.html#function_in
I'm not sure what you are trying to achieve. If ids is a column in informations your code is just a weird way to express "SELECT * FROM informations WHERE ids = ". $id "; If it is a string, I don't see why you need WHERE at all : expression $id in (1,2,3,4,5) is constant, it doesn't require interaction with database; in any case you either grab all rows from informations or none.
UPDATE
Another suggestion :maybe ids is a string field in informations that contains "1,2,3,4,5". In this case you cannot get expected results by using WHERE ... IN. You need to use REGEXP to check if string contains your number.
It has to be column name then IN (comma separated values here).
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...)
You did an error in sql syntax.
This is the correct syntax
"SELECT * FROM informations WHERE ids IN (". $id .")";

MySQL - How to select rows where value is in array?

Ok, normally I know you would do something like this if you knew the array values (1,2,3 in this case):
SELECT * WHERE id IN (1,2,3)
But I don't know the array value, I just know the value I want to find is 'stored' in the array:
SELECT * WHERE 3 IN (ids) // Where 'ids' is an array of values 1,2,3
Which doesn't work. Is there another way to do this?
Use the FIND_IN_SET function:
SELECT t.*
FROM YOUR_TABLE t
WHERE FIND_IN_SET(3, t.ids) > 0
By the time the query gets to SQL you have to have already expanded the list. The easy way of doing this, if you're using IDs from some internal, trusted data source, where you can be 100% certain they're integers (e.g., if you selected them from your database earlier) is this:
$sql = 'SELECT * WHERE id IN (' . implode(',', $ids) . ')';
If your data are coming from the user, though, you'll need to ensure you're getting only integer values, perhaps most easily like so:
$sql = 'SELECT * WHERE id IN (' . implode(',', array_map('intval', $ids)) . ')';
If the array element is not integer you can use something like below :
$skus = array('LDRES10','LDRES12','LDRES11'); //sample data
if(!empty($skus)){
$sql = "SELECT * FROM `products` WHERE `prodCode` IN ('" . implode("','", $skus) . "') "
}
If you use the FIND_IN_SET function:
FIND_IN_SET(a, columnname) yields all the records that have "a" in them, alone or with others
AND
FIND_IN_SET(columnname, a) yields only the records that have "a" in them alone, NOT the ones with the others
So if record1 is (a,b,c) and record2 is (a)
FIND_IN_SET(columnname, a) yields only record2 whereas FIND_IN_SET(a, columnname) yields both records.

Categories