Nested select statement in mysql -my code doesn't work - mysql

here is my query
SELECT con_serial,column2,column3
FROM
(SELECT con_serial,column2,column3
FROM big_table
WHERE ISNULL(contact1, '')+'#'+ISNULL(contact2, '')+'#'+ISNULL(contact3, '')+'#'+ISNULL(contact4, '')+'#'+ISNULL(contact5, '')
LIKE '%' + '".$conserial."' + '%') AS a
WHERE con_serial
IN('".$contact1."','".$contact2."','".$contact3."','".$contact4."','".$contact5."')
at the inner select i wish to get the rows which have this value $conserial in one of their 5 columns(contact1...contact5)
and the outer select to choose the rows from it that their column con_serial is one of the variables ($contact1...$contact5)
can anybody see what's wrong here?

Despite your new formulation, it remains very much unclear.
Nevertheless I'll try to give you an answer, based on what I can guess...
First here is how I'd reformulate your need:
you have some values in PHP variables: one $conserial and five $contact# where # is 1-5
the table structure contains at least these columns: con_serial, column2, column3, and five contact# where # is 1-5
you want to select rows where both (here is the most strange part of your need):
at least one of the contact# columns matches the PHP $conserial value
the con_serial column matches at least one of the PHP $contact# values
That said, note that you don't need to have two nested SELECT: you only want each row to satisfy two conditions, so they can be ANDed in the WHERE clause.
Based on that, your query should be:
$query = "
SELECT con_serial, column2, column3
FROM big_table
WHERE con_serial IN ('$contact1', '$contact2', '$contact3', '$contact4', '$contact5')
AND '$con_serial' IN (contact1, contact2, contact3, contact4, contact5)
";

i guess the sum part in where clause is nut allowed
any way i've solved this with using two IN like this
SELECT con_serial,,column2,column3
FROM(SELECT con_serial,column2,column3
FROM
big_table
WHERE '".$conserial."' IN(contact1,contact2,contact3,contact4,contact5)) a
WHERE con_serial IN('".$contact1."','".$contact2."','".$contact3."','".$contact4."','".$contact5."'
this was wat i wanted Tnx any way ;)

Related

MySQL - WHERE x IN ( column)

I tried something out. Here is a simple example in SQL Fiddle: Example
There is a column someNumbers (comma-seperated numbers) and I tried to get all the rows where this column contains a specific number. Problem is, the result only contains rows where someNumbers starts with the specific number.
The query SELECT * FROM myTable where 2 in ( someNumbers ) only returns the row with id 2 and not the row with id 1.
Any suggestions? Thank you all.
You are storing data in the wrong format! You should not be storing multiple values in a single string column. You should not be storing numbers as strings. Instead, you should have a junction table with one row per id and per number.
Sometimes, you just have no choice, because someone else created a really poorly designed database. For these situations, MySQL has the function find_in_set():
SELECT *
FROM myTable
WHERE find_in_set(2, someNumbers ) > 0;
The right solution, however, is to fix the data model.
While Gordon's answer is a good one, here is a way to do this with like
SELECT * FROM myTable where someNumbers like '2,%' or someNumbers like '%,2,%' or someNumbers like '%,2'
The first like checks if your array starts with the number you are looking for (2). The second one checks if 2 is within the array and the last like tests for appearance at the end.
Note that the commas are essential here, because something like '%2%' would also match ...,123,...
EDIT: As suggested by the OP it may happen that only a single value is present in the row. Consequently, the query must check this case by doing ... someNumbers = '2'
I would suggest this query :
SELECT * FROM myTable where someNumbers like '%2%'
It will select every entry where someNumbers contains '2'
Select * from table_name where coloumn_name IN(value,value,value)
you can use it

Query MySQL field for LIKE string

I have a field called 'areasCovered' in a MySQL database, which contains a string list of postcodes.
There are 2 rows that have similar data e.g:
Row 1: 'B*,PO*,WA*'
Row 2: 'BB*, SO*, DE*'
Note - The strings are not in any particular order and could change depending on the user
Now, if I was to use a query like:
SELECT * FROM technicians WHERE areasCovered LIKE '%B*%'
I'd like it to return JUST Row 1. However, it's returning Row 2 aswell, because of the BB* in the string.
How could I prevent it from doing this?
The key to using like in this case is to include delimiters, so you can look for delimited values:
SELECT *
FROM technicians
WHERE concat(', ', areasCovered, ', ') LIKE '%, B*, %'
In MySQL, you can also use find_in_set(), but the space can cause you problems so you need to get rid of it:
SELECT *
FROM technicians
WHERE find_in_set('B', replace(areasCovered, ', ', ',') > 0
Finally, though, you should not be storing these types of lists as strings. You should be storing them in a separate table, a junction table, with one row per technician and per area covered. That makes these types of queries easier to express and they have better performance.
You are searching wild cards at the start as well as end.
You need only at end.
SELECT * FROM technicians WHERE areasCovered LIKE 'B*%'
Reference:
Normally I hate REGEXP. But ho hum:
SELECT * FROM technicians
WHERE concat(",",replace(areasCovered,", ",",")) regexp ',B{1}\\*';
To explain a bit:
Get rid of the pesky space:
select replace("B*,PO*,WA*",", ",",");
Bolt a comma on the front
select concat(",",replace("B*,PO*,WA*",", ",","));
Use a REGEX to match "comma B once followed by an asterix":
select concat(",",replace("B*,PO*,WA*",", ",",")) regexp ',B{1}\\*';
I could not check it on my machine, but it's should work:
SELECT * FROM technicians WHERE areasCovered <> replace(areaCovered,',B*','whatever')
In case the 'B*' does not exist, the areasCovered will be equal to replace(areaCovered,',B*','whatever'), and it will reject that row.
In case the 'B*' exists, the areCovered will NOT be eqaul to replace(areaCovered,',B*','whatever'), and it will accept that row.
You can Do it the way Programming Student suggested
SELECT * FROM technicians WHERE areasCovered LIKE 'B*%'
Or you can also use limit on query
SELECT * FROM technicians WHERE areasCovered LIKE '%B*%' LIMIT 1
%B*% contains % on each side which makes it to return all the rows where value contains B* at any position of the text however your requirement is to find all the rows which contains values starting with B* so following query should do the work.
SELECT * FROM technicians WHERE areasCovered LIKE 'B*%'

write select query for the below requirement

This is my users table:
http://ezinfotec.com/Capture.PNG
I need to select all rows those are not contain 2 in except column. How to write a query for this using php & Mysql.
The result i expect for this query is only return last row only.
Thank you.
Don't store comma separated values in your table, it's very bad practice, nevertheless you can use FIND_IN_SET
SELECT
*
FROM
users
WHERE
NOT FIND_IN_SET('2', except)
Try this:
SELECT *
FROM users
WHERE CONCAT(',', except, ',') NOT LIKE '%,2,%'
this should work for you
SELECT *
FROM table
WHERE table.except NOT LIKE '%,2%'
OR table.except NOT LIKE '%2,%';

Mysql remove special characters from a set

I want to remove a spacial character in my query can anyone help. This is my query
select sum(value) from table_1 where id in (1, 2,);
This 1,2, is fetch from other table using sub-query.
To remove the trailing colon, you can use trim():
SELECT TRIM(TRAILING ',' FROM '1,2,');
My guess is that you want to look for individual values in the list, especially because ids don't usually contain commas.
For that, you can do:
select sum(value)
from table_1
where find_in_set(id, '1, 2,') > 0;
If the values are coming from a subquery, you would be better off using the subquery directly (in most cases). The query would be something like:
select sum(value)
from table_1
where id in (<subquery>);
You would need to modify the subquery to return a list of ids, rather than all concatenated into one field.

FETCH_ASSOC and SELECT * FROM two tables with the same column name

I have lot of MySQL tables with the same column name. So Im looking for PDO or SQL hack for SELECT * FROM more tables - which will return table names in result sets.
Example:
'SELECT * FROM table0, table1';
Where both tables has 'name' column. But FETCH_ASSOC result returns only one 'name' - the last one.
Result:
echo $result["name"];
Wanted result:
echo $result["table0.name"];
echo $result["table1.name"];
...
Note that
I cannot rename DB columns to be unique
I cannot manualy create alias for each columns (lot of tables/columns)
I want name in result set not numbers like FETCH_NUM
Any ideas?
Thanks!
Hack doesn't exist, you have to create aliases.
You said that you don't want to alias all columns because there are too many, but have you considered only aliasing the ones that give you problems?
SELECT
*,
table0.name AS t0name,
table1.name AS t1name
FROM table0 JOIN table1 ON ...