mysql how to select all and exclude one column from table [duplicate] - php
I'm trying to use a select statement to get all of the columns from a certain MySQL table except one. Is there a simple way to do this?
EDIT: There are 53 columns in this table (NOT MY DESIGN)
Actually there is a way, you need to have permissions of course for doing this ...
SET #sql = CONCAT('SELECT ', (SELECT REPLACE(GROUP_CONCAT(COLUMN_NAME), '<columns_to_omit>,', '') FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '<table>' AND TABLE_SCHEMA = '<database>'), ' FROM <table>');
PREPARE stmt1 FROM #sql;
EXECUTE stmt1;
Replacing <table>, <database> and <columns_to_omit>
(Do not try this on a big table, the result might be... surprising !)
TEMPORARY TABLE
DROP TABLE IF EXISTS temp_tb;
CREATE TEMPORARY TABLE ENGINE=MEMORY temp_tb SELECT * FROM orig_tb;
ALTER TABLE temp_tb DROP col_a, DROP col_f,DROP col_z; #// MySQL
SELECT * FROM temp_tb;
DROP syntax may vary for databases #Denis Rozhnev
Would a View work better in this case?
CREATE VIEW vwTable
as
SELECT
col1
, col2
, col3
, col..
, col53
FROM table
You can do:
SELECT column1, column2, column4 FROM table WHERE whatever
without getting column3, though perhaps you were looking for a more general solution?
If you are looking to exclude the value of a field, e.g. for security concerns / sensitive info, you can retrieve that column as null.
e.g.
SELECT *, NULL AS salary FROM users
To the best of my knowledge, there isn't. You can do something like:
SELECT col1, col2, col3, col4 FROM tbl
and manually choose the columns you want. However, if you want a lot of columns, then you might just want to do a:
SELECT * FROM tbl
and just ignore what you don't want.
In your particular case, I would suggest:
SELECT * FROM tbl
unless you only want a few columns. If you only want four columns, then:
SELECT col3, col6, col45, col 52 FROM tbl
would be fine, but if you want 50 columns, then any code that makes the query would become (too?) difficult to read.
While trying the solutions by #Mahomedalid and #Junaid I found a problem. So thought of sharing it. If the column name is having spaces or hyphens like check-in then the query will fail. The simple workaround is to use backtick around column names. The modified query is below
SET #SQL = CONCAT('SELECT ', (SELECT GROUP_CONCAT(CONCAT("`", COLUMN_NAME, "`")) FROM
INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'users' AND COLUMN_NAME NOT IN ('id')), ' FROM users');
PREPARE stmt1 FROM #SQL;
EXECUTE stmt1;
If the column that you didn't want to select had a massive amount of data in it, and you didn't want to include it due to speed issues and you select the other columns often, I would suggest that you create a new table with the one field that you don't usually select with a key to the original table and remove the field from the original table. Join the tables when that extra field is actually required.
You could use DESCRIBE my_table and use the results of that to generate the SELECT statement dynamically.
My main problem is the many columns I get when joining tables. While this is not the answer to your question (how to select all but certain columns from one table), I think it is worth mentioning that you can specify table. to get all columns from a particular table, instead of just specifying .
Here is an example of how this could be very useful:
select users.*, phone.meta_value as phone, zipcode.meta_value as zipcode
from users
left join user_meta as phone
on ( (users.user_id = phone.user_id) AND (phone.meta_key = 'phone') )
left join user_meta as zipcode
on ( (users.user_id = zipcode.user_id) AND (zipcode.meta_key = 'zipcode') )
The result is all the columns from the users table, and two additional columns which were joined from the meta table.
I liked the answer from #Mahomedalid besides this fact informed in comment from #Bill Karwin. The possible problem raised by #Jan Koritak is true I faced that but I have found a trick for that and just want to share it here for anyone facing the issue.
we can replace the REPLACE function with where clause in the sub-query of Prepared statement like this:
Using my table and column name
SET #SQL = CONCAT('SELECT ', (SELECT GROUP_CONCAT(COLUMN_NAME) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'users' AND COLUMN_NAME NOT IN ('id')), ' FROM users');
PREPARE stmt1 FROM #SQL;
EXECUTE stmt1;
So, this is going to exclude only the field id but not company_id
Yes, though it can be high I/O depending on the table here is a workaround I found for it.
SELECT *
INTO #temp
FROM table
ALTER TABLE #temp DROP COlUMN column_name
SELECT *
FROM #temp
It is good practice to specify the columns that you are querying even if you query all the columns.
So I would suggest you write the name of each column in the statement (excluding the one you don't want).
SELECT
col1
, col2
, col3
, col..
, col53
FROM table
I agree with the "simple" solution of listing all the columns, but this can be burdensome, and typos can cause lots of wasted time. I use a function "getTableColumns" to retrieve the names of my columns suitable for pasting into a query. Then all I need to do is to delete those I don't want.
CREATE FUNCTION `getTableColumns`(tablename varchar(100))
RETURNS varchar(5000) CHARSET latin1
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE res VARCHAR(5000) DEFAULT "";
DECLARE col VARCHAR(200);
DECLARE cur1 CURSOR FOR
select COLUMN_NAME from information_schema.columns
where TABLE_NAME=#table AND TABLE_SCHEMA="yourdatabase" ORDER BY ORDINAL_POSITION;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur1;
REPEAT
FETCH cur1 INTO col;
IF NOT done THEN
set res = CONCAT(res,IF(LENGTH(res)>0,",",""),col);
END IF;
UNTIL done END REPEAT;
CLOSE cur1;
RETURN res;
Your result returns a comma delimited string, for example...
col1,col2,col3,col4,...col53
I agree that it isn't sufficient to Select *, if that one you don't need, as mentioned elsewhere, is a BLOB, you don't want to have that overhead creep in.
I would create a view with the required data, then you can Select * in comfort --if the database software supports them. Else, put the huge data in another table.
At first I thought you could use regular expressions, but as I've been reading the MYSQL docs it seems you can't. If I were you I would use another language (such as PHP) to generate a list of columns you want to get, store it as a string and then use that to generate the SQL.
Based on #Mahomedalid answer, I have done some improvements to support "select all columns except some in mysql"
SET #database = 'database_name';
SET #tablename = 'table_name';
SET #cols2delete = 'col1,col2,col3';
SET #sql = CONCAT(
'SELECT ',
(
SELECT GROUP_CONCAT( IF(FIND_IN_SET(COLUMN_NAME, #cols2delete), NULL, COLUMN_NAME ) )
FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = #tablename AND TABLE_SCHEMA = #database
),
' FROM ',
#tablename);
SELECT #sql;
If you do have a lots of cols, use this sql to change group_concat_max_len
SET ##group_concat_max_len = 2048;
I agree with #Mahomedalid's answer, but I didn't want to do something like a prepared statement and I didn't want to type all the fields, so what I had was a silly solution.
Go to the table in phpmyadmin->sql->select, it dumps the query: copy, replace and done! :)
While I agree with Thomas' answer (+1 ;)), I'd like to add the caveat that I'll assume the column that you don't want contains hardly any data. If it contains enormous amounts of text, xml or binary blobs, then take the time to select each column individually. Your performance will suffer otherwise. Cheers!
Just do
SELECT * FROM table WHERE whatever
Then drop the column in you favourite programming language: php
while (($data = mysql_fetch_array($result, MYSQL_ASSOC)) !== FALSE) {
unset($data["id"]);
foreach ($data as $k => $v) {
echo"$v,";
}
}
The answer posted by Mahomedalid has a small problem:
Inside replace function code was replacing "<columns_to_delete>," by "", this replacement has a problem if the field to replace is the last one in the concat string due to the last one doesn't have the char comma "," and is not removed from the string.
My proposal:
SET #sql = CONCAT('SELECT ', (SELECT REPLACE(GROUP_CONCAT(COLUMN_NAME),
'<columns_to_delete>', '\'FIELD_REMOVED\'')
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = '<table>'
AND TABLE_SCHEMA = '<database>'), ' FROM <table>');
Replacing <table>, <database> and `
The column removed is replaced by the string "FIELD_REMOVED" in my case this works because I was trying to safe memory. (The field I was removing is a BLOB of around 1MB)
You can use SQL to generate SQL if you like and evaluate the SQL it produces. This is a general solution as it extracts the column names from the information schema. Here is an example from the Unix command line.
Substituting
MYSQL with your mysql command
TABLE with the table name
EXCLUDEDFIELD with excluded field name
echo $(echo 'select concat("select ", group_concat(column_name) , " from TABLE") from information_schema.columns where table_name="TABLE" and column_name != "EXCLUDEDFIELD" group by "t"' | MYSQL | tail -n 1) | MYSQL
You will really only need to extract the column names in this way only once to construct the column list excluded that column, and then just use the query you have constructed.
So something like:
column_list=$(echo 'select group_concat(column_name) from information_schema.columns where table_name="TABLE" and column_name != "EXCLUDEDFIELD" group by "t"' | MYSQL | tail -n 1)
Now you can reuse the $column_list string in queries you construct.
I wanted this too so I created a function instead.
public function getColsExcept($table,$remove){
$res =mysql_query("SHOW COLUMNS FROM $table");
while($arr = mysql_fetch_assoc($res)){
$cols[] = $arr['Field'];
}
if(is_array($remove)){
$newCols = array_diff($cols,$remove);
return "`".implode("`,`",$newCols)."`";
}else{
$length = count($cols);
for($i=0;$i<$length;$i++){
if($cols[$i] == $remove)
unset($cols[$i]);
}
return "`".implode("`,`",$cols)."`";
}
}
So how it works is that you enter the table, then a column you don't want or as in an array: array("id","name","whatevercolumn")
So in select you could use it like this:
mysql_query("SELECT ".$db->getColsExcept('table',array('id','bigtextcolumn'))." FROM table");
or
mysql_query("SELECT ".$db->getColsExcept('table','bigtextcolumn')." FROM table");
May be I have a solution to Jan Koritak's pointed out discrepancy
SELECT CONCAT('SELECT ',
( SELECT GROUP_CONCAT(t.col)
FROM
(
SELECT CASE
WHEN COLUMN_NAME = 'eid' THEN NULL
ELSE COLUMN_NAME
END AS col
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'employee' AND TABLE_SCHEMA = 'test'
) t
WHERE t.col IS NOT NULL) ,
' FROM employee' );
Table :
SELECT table_name,column_name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'employee' AND TABLE_SCHEMA = 'test'
================================
table_name column_name
employee eid
employee name_eid
employee sal
================================
Query Result:
'SELECT name_eid,sal FROM employee'
I use this work around although it may be "Off topic" - using mysql workbench and the query builder -
Open the columns view
Shift select all the columns you want in your query (in your case all but one which is what i do)
Right click and select send to SQL Editor-> name short.
Now you have the list and you can then copy paste the query to where ever.
If it's always the same one column, then you can create a view that doesn't have it in it.
Otherwise, no I don't think so.
I would like to add another point of view in order to solve this problem, specially if you have a small number of columns to remove.
You could use a DB tool like MySQL Workbench in order to generate the select statement for you, so you just have to manually remove those columns for the generated statement and copy it to your SQL script.
In MySQL Workbench the way to generate it is:
Right click on the table -> send to Sql Editor -> Select All Statement.
The accepted answer has several shortcomings.
It fails where the table or column names requires backticks
It fails if the column you want to omit is last in the list
It requires listing the table name twice (once for the select and another for the query text) which is redundant and unnecessary
It can potentially return column names in the wrong order
All of these issues can be overcome by simply including backticks in the SEPARATOR for your GROUP_CONCAT and using a WHERE condition instead of REPLACE(). For my purposes (and I imagine many others') I wanted the column names returned in the same order that they appear in the table itself. To achieve this, here we use an explicit ORDER BY clause inside of the GROUP_CONCAT() function:
SELECT CONCAT(
'SELECT `',
GROUP_CONCAT(COLUMN_NAME ORDER BY `ORDINAL_POSITION` SEPARATOR '`,`'),
'` FROM `',
`TABLE_SCHEMA`,
'`.`',
TABLE_NAME,
'`;'
)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE `TABLE_SCHEMA` = 'my_database'
AND `TABLE_NAME` = 'my_table'
AND `COLUMN_NAME` != 'column_to_omit';
I have a suggestion but not a solution.
If some of your columns have a larger data sets then you should try with following
SELECT *, LEFT(col1, 0) AS col1, LEFT(col2, 0) as col2 FROM table
If you use MySQL Workbench you can right-click your table and click Send to sql editor and then Select All Statement This will create an statement where all fields are listed, like this:
SELECT `purchase_history`.`id`,
`purchase_history`.`user_id`,
`purchase_history`.`deleted_at`
FROM `fs_normal_run_2`.`purchase_history`;
SELECT * FROM fs_normal_run_2.purchase_history;
Now you can just remove those that you dont want.
Related
Summing columns x through x+n in an SQL table
I'm trying to sum columns x through x+n in an SQL table. Essentially, I have multiple tables that contain grades in them and a user_id. I want to sum all the grades to come up with a total grade column without specifying the column names as the names and number of columns changes with each table. For instance, one table might have columns (user_id, calculations, prelab, deductions) while another might have (user_id, accuracy, precision, graphs, prelab, deductions). I could rename my columns col1, col2, col3, col4, col5, etc., but I can't figure out how to get around the varying number of columns.
As far as I know, there is no way to sum groups of columns without actually specifying the column names directly in SQL. It seems to me like this is a badly designed schema, but that's a separate topic. In any your case, you're going to need to create a new column in each table that contains the sum of all the grades in that particular table, say called total, and then, do something like this: select user_id, sum(table1.total, table2.total, table3.total) from table1, table2, table3 where table1.user_id = table2.user_id and table2.user_id = table3.user_id group by user_id
1) You could write some pl/sql to go and hit the data dictionary and get the columns and then construct dynamic sql to do the work of adding them up correctly. 2) Or you could create views on top of the tables that contain the user_id and the sum of the interesting columns (the views themselves could be constructed programmatically - but that only needs to happen once rather than every time you want the totals). But either of the above is probably over-kill compared to simply fixing your schema.
The following procedure would likely do the trick. It will look for all column names for the given tableName in the INFORMATION_SCHEMA.COLUMNS table (excluding 'userid' - This may be subject to change if the name you use is different). The procedure also creates a temporary table (this is also subject to improvement - it would probably be better to do a 'drop if exists before the create) to store the sum up to a point. The items inside the loop is just building an SQL UPDATE statement with the given tableName argument and the columnName from the cursor and doing the math. To test this (after creation): call myProcedure('tableName'); DELIMITER // DROP PROCEDURE IF EXISTS myProcedure // CREATE PROCEDURE myProcedure( tableName varchar(32) ) BEGIN DECLARE done INT DEFAULT FALSE; DECLARE columnName varchar(64); DECLARE cur1 CURSOR FOR SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = tableName and COLUMN_NAME <> 'userid'; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; OPEN cur1; CREATE TEMPORARY TABLE intermediateresults(userid integer, sumOfScores integer); SET #st1 = CONCAT('INSERT INTO intermediateresults (SELECT DISTINCT userid, 0 FROM ',tableName,' )' ); PREPARE stmt3 FROM #st1; EXECUTE stmt3; looping: LOOP FETCH cur1 into columnName; IF done THEN LEAVE looping; END IF; SET #st1 = CONCAT('UPDATE intermediateresults set sumOfScores = sumOfScores + COALESCE( (SELECT ', columnName, ' FROM ',tableName, ' t WHERE t.userid=intermediateresults.userid) , 0)' ); PREPARE stmt3 FROM #st1; EXECUTE stmt3; DEALLOCATE PREPARE stmt3; END LOOP; CLOSE cur1; SELECT * FROM intermediateresults; DROP table intermediateresults; END // DELIMITER ; What might be of interest when doing this kind of thing: INFORMATION_SCHEMA also has data on: DATA_TYPE: which can be used to test if a specific column has the actual type you are expecting - a condition such as DATA_TYPE='int' can be added to the cursor definition to make sure that it is in fact an int (assuming that the columns to be summed are in fact INTs) ORDINAL_POSITION: which can be used if you know in which order the columns are supposed to arrive (for cases where the last four are housekeeping, for instance) TABLE_SCHEMA: the procedure above rather assumes that the table is only present in the current default schema. Using this would require an additional parameter in the procedure and a slight change in the constructed SQL statements.
Query from a Table, That's derived from a Information_Schema.Tables Result
I'm having to query from a Database that has more than 50 tables - all having the same structure (I know, Horrid Database design from a legacy project that's been in production for 5+ years!). To do this, I've queried the information_Schema like below: SELECT TABLE_NAME FROM INFORMATION_SCHEMA.tables WHERE TABLE_SCHEMA = 'projectdatabase' AND TABLE_NAME LIKE '%_usertable' Which provides me the 50 or so tables that I need, in a result. Now, I will need to query columns from within each of those tables for example, PRODUCT_ID. In doing so, I've attempted: SELECT projectdatabase.userTable.PRODUCT_ID FROM ( SELECT TABLE_NAME as userTable FROM INFORMATION_SCHEMA.tables WHERE TABLE_SCHEMA = 'projectdatabase' AND TABLE_NAME LIKE '%_usertable' ) AS userTables Now this obviously doesn't work due to MySQL not treating the 'userTable' as Database table - but what I'm attempting to do, is query * FROM {tablename} where tablename was the information_schema query result. I could attempt to split this up in PHP, although I'm eagerly wondering if this was possible to do within MySQL.
You have to use prepared statement: SET #sql:=(SELECT GROUP_CONCAT( CONCAT("SELECT PRODUCT_ID FROM ", TABLE_NAME) SEPARATOR " UNION ") FROM INFORMATION_SCHEMA.tables WHERE TABLE_SCHEMA = 'projectdatabase' AND TABLE_NAME LIKE '%_usertable'); PREPARE stmt FROM #sql; EXECUTE stmt; EDIT: You can also set SET SESSION group_concat_max_len = 1000000;, but doing all in SQL is not a thing to do. Your table list is kind of a constant, and the query would be in a better place in your PHP code.
Adam deserves the bounty. The group_concat_max_len can be captured and restored if you like. And you can make it even larger. So I don't see a reliability issue. And I agree that you cannot have the tablename be a 'variable'. The PHP equivalent would go something like: Get the list of tables from SELECT TABLE_NAME FROM INFORMATION_SCHEMA.tables WHERE TABLE_SCHEMA = 'projectdatabase' AND TABLE_NAME LIKE '%_usertable'; Either build the UNION, Adam's suggestion, or run the 50 SELECT's, each based on one tablename from the above list. Then combine them however you want. Recommend thinking through UNION DISTINCT versus UNION ALL.
SQL MAX in conjunction with WHERE clause
I need to find the largest value from one particular column in a mysql table where the value of another column is equivalent to something. But, with the query that I'm using, I keep on getting a message that displays and SQL error. My query is aS follows: SELECT MAX(message_id) AS top_message HAVING(child_id) = '".$message_id[$i]."' Any suggestions?
You are also missing a table name: SELECT MAX(message_id) AS top_message FROM tablename WHERE child_id = '".$message_id[$i]."'
You should use WHERE instead of HAVING Clause: SELECT MAX(message_id) AS top_message FROM tablename WHERE child_id = '".$message_id[$i]."' Use only HAVING clause when you have an aggregated conditon.
You need a from clause and a where clause. The having clause is used for group filters. You don't have a group by clause, so there is no reason to write a having clause. If the table where you want to select from is called 'MyTable', then your query is as follows: SELECT MAX(message_id) AS top_message FROM MyTable WHERE child_id = '".$message_id[$i]."' Note, that the paranthesis around child_id is not needed. Please read SQL and MySQL tutorials for more information, your life will be much easier.
Select on empty table but still get column names
I want to do a SELECT on an empty table, but i still want to get a single record back with all the column names. I know there are other ways to get the column names from a table, but i want to know if it's possible with some sort of SELECT query. I know this one works when i run it directly in MySQL: SELECT * FROM cf_pagetree_elements WHERE 1=0; But i'm using PHP + PDO (FETCH_CLASS). This just gives me an empty object back instead of an row with all the column names (with empty values). So for some reason that query doesn't work with PDO FETCH_CLASS. $stmt = $this->db->prepare ( $sql ); $stmt->execute ( $bindings ); $result = $stmt->fetchAll ( \PDO::FETCH_CLASS, $class ); print_r($result); // Empty object... I need an object with column names Anyone any idea if there's another method that i can try?
Adding on to what w00 answered, there's a solution that doesn't even need a dummy table SELECT tbl.* FROM (SELECT 1) AS ignore_me LEFT JOIN your_table AS tbl ON 1 = 1 LIMIT 1 In MySQL you can change WHERE 1 = 1 to just WHERE 1
To the other answers who posted about SHOW COLUMNS and the information scheme. The OP clearly said: "I know there are other ways to get the column names from a table, but i want to know if it's possible with some sort of SELECT query." Learn to read. Anyway, to answer your question; No you can't. You cannot select a row from an empty table. Not even a row with empty values, from an empty table. There is however a trick you can apply to do this. Create an additional table called 'dummy' with just one column and one row in it: Table: dummy dummy_id: 1 That's all. Now you can do a select statement like this: SELECT * FROM dummy LEFT OUTER JOIN your_table ON 1=1 This will always return one row. It does however contain the 'dummy_id' column too. You can however just ignore that ofcourse and do with the (empty) data what ever you like. So again, this is just a trick to do it with a SELECT statement. There's no default way to get this done.
SHOW COLUMNS FROM cf_pagetree_elements; This will give a result set explaining the table structure. You can quite easily parse the result with PHP. Another method is to query the infomrmation schema table: SELECT column_name FROM information_schema.columns WHERE table_name='cf_pagetree_elements'; Not really recommended though!
You could try: SELECT * FROM information_schema.columns WHERE table_name = "cf_pagetree_elements" Not sure about your specific PHP+PDO approach (there may be complications), but that's the standard way to fetch column headings (field names).
this will list the columns of ANY query for PDO drivers that support getColumMeta. I am using this with SQL server and works fine even on very complex queries with aliased tables, sub-queries and unions. Gives me columns even when results are zero <?php // just an example of an empty query. $query =$PDOdb->query("SELECT * from something where 1=0; "); for ($i=0; $i<$query->columnCount(); $i++) { echo $query->getColumnMeta($i)['name']."<br />"; } ?>
Even without PDO in the way, the database won't return the structure without at least one row. You could do this and ignore the data row: SELECT * FROM cf_pagetree_elements LIMIT 1; Or you could simply DESC cf_pagetree_elements; and deal with one row per field. WHERE 1=0 does not work for me. It always returns empty set.
The latest PDO for SQLSVR definitely works with get column meta. Simply set up your statement and use this to get an array of useful information: $stmt->execute(); $meta= array(); foreach(range(0, $stmt->columnCount() - 1) as $column_index) { array_push($meta,$stmt->getColumnMeta($column_index)); }
Complete solution for Oracle or MySQL for any or some columns (my goal is to get arbitrary columns exactly as they are in DB regardless of case) for any table (w or w/o rows) $qr = <<<SQL SELECT $cols FROM (SELECT NULL FROM DUAL) LEFT JOIN $able t ON 1 = 0 SQL; $columns = array_keys($con->query($qr)->fetchAll(PDO::FETCH_ASSOC)[0]); if($cols === "*") { array_shift($columns); }
YOu could use MetaData with; $cols = mysql_query("SHOW COLUMNS FROM $tableName", $conn);
Comma separated values in MySQL "IN" clause
I have a column in one of my table where I store multiple ids seperated by comma's. Is there a way in which I can use this column's value in the "IN" clause of a query. The column(city) has values like 6,7,8,16,21,2 I need to use as select * from table where e_ID in (Select city from locations where e_Id=?) I am satisfied with Crozin's answer, but I am open to suggestions, views and options. Feel free to share your views.
Building on the FIND_IN_SET() example from #Jeremy Smith, you can do it with a join so you don't have to run a subquery. SELECT * FROM table t JOIN locations l ON FIND_IN_SET(t.e_ID, l.city) > 0 WHERE l.e_ID = ? This is known to perform very poorly, since it has to do table-scans, evaluating the FIND_IN_SET() function for every combination of rows in table and locations. It cannot make use of an index, and there's no way to improve it. I know you said you are trying to make the best of a bad database design, but you must understand just how drastically bad this is. Explanation: Suppose I were to ask you to look up everyone in a telephone book whose first, middle, or last initial is "J." There's no way the sorted order of the book helps in this case, since you have to scan every single page anyway. The LIKE solution given by #fthiella has a similar problem with regards to performance. It cannot be indexed. Also see my answer to Is storing a delimited list in a database column really that bad? for other pitfalls of this way of storing denormalized data. If you can create a supplementary table to store an index, you can map the locations to each entry in the city list: CREATE TABLE location2city ( location INT, city INT, PRIMARY KEY (location, city) ); Assuming you have a lookup table for all possible cities (not just those mentioned in the table) you can bear the inefficiency one time to produce the mapping: INSERT INTO location2city (location, city) SELECT l.e_ID, c.e_ID FROM cities c JOIN locations l ON FIND_IN_SET(c.e_ID, l.city) > 0; Now you can run a much more efficient query to find entries in your table: SELECT * FROM location2city l JOIN table t ON t.e_ID = l.city WHERE l.e_ID = ?; This can make use of an index. Now you just need to take care that any INSERT/UPDATE/DELETE of rows in locations also inserts the corresponding mapping rows in location2city.
From MySQL's point of view you're not storing multiple ids separated by comma - you're storing a text value, which has the exact same meaing as "Hello World" or "I like cakes!" - i.e. it doesn't have any meaing. What you have to do is to create a separated table that will link two objects from the database together. Read more about many-to-many or one-to-many (depending on your requirements) relationships in SQL-based databases.
Rather than use IN on your query, use FIND_IN_SET (docs): SELECT * FROM table WHERE 0 < FIND_IN_SET(e_ID, ( SELECT city FROM locations WHERE e_ID=?)) The usual caveats about first form normalization apply (the database shouldn't store multiple values in a single column), but if you're stuck with it, then the above statement should help.
This does not use IN clause, but it should do what you need: Select * from table where CONCAT(',', (Select city from locations where e_Id=?), ',') LIKE CONCAT('%,', e_ID, ',%') but you have to make sure that e_ID does not contain any commas or any jolly character. e.g. CONCAT(',', '6,7,8,16,21,2', ',') returns ',6,7,8,16,21,2,' e_ID=1 --> ',6,7,8,16,21,2,' LIKE '%,1,%' ? FALSE e_ID=6 --> ',6,7,8,16,21,2,' LIKE '%,6,%' ? TRUE e_ID=21 --> ',6,7,8,16,21,2,' LIKE '%,21,%' ? TRUE e_ID=2 --> ',6,7,8,16,21,2,' LIKE '%,2,%' ? TRUE e_ID=3 --> ',6,7,8,16,21,2,' LIKE '%,3,%' ? FALSE etc.
Don't know if this is what you want to accomplish. With MySQL there is feature to concatenate values from a group GROUP_CONCAT You can try something like this: select * from table where e_ID in (Select GROUP_CONCAT(city SEPARATOR ',') from locations where e_Id=?)
this one in for oracle ..here string concatenation is done by wm_concat select * from table where e_ID in (Select wm_concat(city) from locations where e_Id=?) yes i agree with raheel shan .. in order put this "in" clause we need to make that column into row below code one do that job. select * from table where to_char(e_ID) in ( select substr(city,instr(city,',',1,rownum)+1,instr(city,',',1,rownum+1)-instr(city,',',1,rownum)-1) from ( select ','||WM_CONCAT(city)||',' city,length(WM_CONCAT(city))-length(replace(WM_CONCAT(city),','))+1 CNT from locations where e_Id=? ) TST ,ALL_OBJECTS OBJ where TST.CNT>=rownum ) ;
you should use FIND_IN_SET Returns position of value in string of comma-separated values mysql> SELECT FIND_IN_SET('b','a,b,c,d'); -> 2
You need to "SPLIT" the city column values. It will be like: SELECT * FROM table WHERE e_ID IN (SELECT TO_NUMBER( SPLIT_STR(city /*string*/ , ',' /*delimiter*/ , 1 /*start_position*/ ) ) FROM locations); You can read more about the MySQL split_str function here: http://blog.fedecarg.com/2009/02/22/mysql-split-string-function/ Also, I have used the TO_NUMBER function of Oracle here. Please replace it with a proper MySQL function.
IN takes rows so taking comma seperated column for search will not do what you want but if you provide data like this ('1','2','3') this will work but you can not save data like this in your field whatever you insert in the column it will take the whole thing as a string.
You can create a prepared statement dynamically like this set #sql = concat('select * from city where city_id in (', (select cities from location where location_id = 3), ')'); prepare in_stmt from #sql; execute in_stmt; deallocate prepare in_stmt;
Ref: Use a comma-separated string in an IN () in MySQL Recently I faced the same problem and this is how I resolved it. It worked for me, hope this is what you were looking for. select * from table_name t where (select (CONCAT(',',(Select city from locations l where l.e_Id=?),',')) as city_string) LIKE CONCAT('%,',t.e_ID,',%'); Example: It will look like this select * from table_name t where ',6,7,8,16,21,2,' LIKE '%,2,%';