I have a SQL Select query in PHP where i need to lookup rows in a table where a column is like a string with and without spaces
for example, if my value in the database is ABC 123 and i search for ABC123 the query will be
$sql = SELECT * from table where col_name LIKE '%ABC123%' ";
but i need it to return the row where col_name equals ABC 123
and vice versa, for example, if the database value is 123ABC and i search for 123 ABC the query will be
$sql = SELECT * from table where col_name LIKE '%123 ABC%' ";
and i need it to return the row where col_name equals 123ABC
$sql = SELECT * from table where REPLACE(col_name,' ','') LIKE '%ABC123%' ";
Related
I have an SQL table where there are three columns: id, name, and group. I want to get the value of group from below example:
Id = 1
Name = abc
Group = 1
I need an SQL query from which I can get value abc on fetching group=1.
You should use something like this:
SELECT Name
FROM table_name_here
WHERE table_name_here.Group = 1
You can do if you want to strict filter your request using operator AND/OR.
SELECT id, name, group FROM TABLENAME
WHERE id = 1 AND name = "abc" AND group = 1;
How to retrieve / output values that match multiple table fields.
SELECT name FROM table WHERE name IN ( 'Value1', 'Value2' );
My search query should take the following parameters : first_name and roll_number in user_table.
e.g. Retrieving a query on the following lines : Roy whose roll number is 5
I need to query this using a single query.
Consider writing the following sql statement:
SELECT name FROM table_name WHERE first_name IN ('Roy') AND roll_number = 5;
You can alternatively try the following query:
SELECT name FROM table_name WHERE first_name = 'Roy' AND roll_number = 5;
Select * from tableName
where roll_number =5
and first_name ='ROY'
--replace 5 and ROY with parameter
Example: I want sql to output only rows that have 6 digit of character 'AAAAAA' I don't want rows that have more than that.
"SELECT * FROM table WHERE LENGTH(columnName) = 6" If MySql and
"SELECT * FROM table WHERE LEN(columnName) = 6" If MSSql
SELECT * FROM table WHERE field REGEXP '^[A]{6}$';
This is my table:
ID NAME GROUP
123456 Example 1
789012 Test 2
345678 Lorem 1
This code works fine:
select * from mytable where id="789012"
However, this code fails:
select * from mytable where group="1"
Why is this? Isn't the whole point of iterating with the while loop to return multiple rows?
Your query fails. You need to escape reserved words in MySQL like group with backticks
select * from mytable where `group` = 1
i am trying to run this query. but it shows either 1 row or give error query result having more than 1 row.
select * from abc where id=(select nameid from xyz where name like '%abc%')
help me.
i am executing a query where i get name, father name and schoolid from table abc. now i want school name from table2 by running another query in it but in all time it shows 1
Use the IN to fetch the more than 1 row:
select * from abc where id IN (select nameid from xyz where name like '%abc%');
EDIT:
select a.*, x.school_name from abc a left join xyz x on a.id = x.nameid where name like '%abc%';
try this with IN to get more then one row
select * from `abc` where id in (select nameid from xyz where name like '%abc%')
This should be
select * from abc where id IN (select nameid from xyz where name like '%abc%')
Use IN mysql clause.
SELECT * FROM abc WHERE id IN (SELECT nameid FROM xyz WHERE name LIKE '%abc%')
because where condition only check for one single value but you were comparing it with array of data returned by subquery.
What you were doin is
if($value = array(1,2,3))
which will always return false so you need to use something which enable you to look into array and give the result.
In other word comparison will only perform on once value like one = one not on one = array('one',two)
so IN clause will do that in MySQL.