need rows with specific column entries in MySQL - php

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'

Related

select rows containing a string in a specific column

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

how to check that a field in mysql contains a specific word

I need to check if a field in mysql contains a specific word using select query
exmaple: field 'name' = test1,test2,test3
Select * from table where name Like '%test3%
it returns empty, any help
use find_in_set():
SELECT * from `table` where FIND_IN_SET(name, 'test1,test2,test3 ');
use this to find word like test1,test2 etc
Select * from table where name Like '%test%'
Try this:-
SELECT * FROM table WHERE name IN ( 'test1',' test2', 'test3');
Add single quotes after '%test3%:
SELECT * FROM table WHERE name LIKE '%test3%

Select multiple rows from MySQL table

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

Can I remove an apostrophe from an SQL search (WHERE clause)?

I have a query in SQL (Mysql) using a where clause.
SELECT * FROM TABLE WHERE name = 'Bristols';
Now I know that there's a row in the table containing Bristol's with an apostrophe, but not one without an apostrophe. However I want to return the row anyway. The problem is that I can only feed the query a value without an apostrophe: Bristols - is there any way within the query to remove the apostrophe from the field the query is searching?
SELECT * FROM TABLE
WHERE replace(name, '''', '') = 'Bristols'
There are several ways to accomplish this:
See Fiddle
Regex:
SELECT *
FROM cities
WHERE name REGEXP 'Bristol\'?s';
Replace:
SELECT *
FROM cities
WHERE 'Bristols' = replace(name,'\'','');
Explicit Matching:
SELECT *
FROM cities
WHERE name IN('Bristols','Bristol''s');
You have Two possible outlooks:
First:
SELECT * FROM TABLE WHERE name LIKE '%Bristol%' // Gather data like: BrISTOLS, Bristols, Bristol, Bristol's,
Second:
SELECT * FROM TABLE WHERE replace(name,'''','') = 'Bristols'

SELECT * FROM Table WHERE Column LIKE %?%

Based on information here MySQL query String contains
trying to create pdo query with ?
Experimented with following
SELECT * FROM Table WHERE Column LIKE %?%
SELECT * FROM Table WHERE Column LIKE ?%
SELECT * FROM Table WHERE Column LIKE %?
Nothing works. Get error
Syntax error or access violation: 1064 You have an error in your SQL
syntax; check the manual that corresponds to your MySQL server version
for the right syntax to use near '%...
Tried with SELECT * FROM Table WHERE Column LIKE ? but this is not for contains
Aim is to get query SELECT * FROM Table WHERE Column contains ?
What is is correct pdo contains statement for positional placeholders (?)?
try this by concatenating the value in the string via PHP,
$value = "valueHere";
$passThis = "%" . $value . "%";
// other pdo codes...
$stmt = $dbh->prepare("SELECT * FROM Table WHERE Column LIKE ?");
$stmt->bindParam(1, $passThis);
// other pdo codes...
after like add quotes. eg:- like '%?%'
i.e:
SELECT * FROM table_name WHERE column_name like '%field_name%';
I think wildcard stament should be within single quotes, like;
SELECT * FROM Table WHERE Column LIKE '%?%';
This returns any record which contains the string given anywhere within the particular column field
Column data which starts with 'ber', example
SELECT * FROM Table WHERE Column LIKE 'ber%';
Hope this helps
Either put the % characters before and after your parameter before you pass it into the query or
SELECT * FROM Table WHERE Column LIKE '%' + ? + '%'
SELECT * FROM Table WHERE Column LIKE ? + '%'
SELECT * FROM Table WHERE Column LIKE '%' + ?
Although this will fail if ? is null because the concatenate will yield null. you could use coalesce
SELECT * FROM Table WHERE Column LIKE '%' + Coalesce(?,'') + '%'
SELECT * FROM Table WHERE Column LIKE Coalesce(?,'') + '%'
SELECT * FROM Table WHERE Column LIKE '%' + Coalesce(?,'')
If you would like to use prepared statements then take a look at http://php.net/manual/de/pdo.prepared-statements.php.
SELECT * FROM REGISTRY where name LIKE '%?%'

Categories