Check for word in table field MySql - mysql

I have a column with field ids
ids contains value like
1,2,3,4,....
Now I want to check an Id=4 in ids field

You can use find_in_set
select * from your_table
where find_in_set(4, ids) > 0
But actually you should rather change your table design. Never store multiple values in a single column.

You can also used array for Checking it. but the stracture of the table should have to changed it would be good.

Related

SELECT all COLUMN_NAMES from a table that have a specific entry

I have a table with 21 columns and I need to find all column names that have a specific value in them. This goes beyond my knowledge of database querying.
If this is possible, how would I do this?
Do you need to find columns in which the name of the column contains a certain value or there exists a row in which the given column contains the given value?
In the first case you could try the show columns: http://dev.mysql.com/doc/refman/5.0/en/show-columns.html

Mysql check if column in a row is contained in another column?

I'm trying to make a mysql query that checks if a column in a row is contained within another column in the same row. Is there a way to do that kinda of query?
for example:
Key Value runHash
2500 tacos night.2500.293849284
1775 windows day.176555.43035842
I am trying to write a query that will return the second row and not the first because for the first row, Key is in runHash.
I tried to do:
select * from table where key not in runHash
However this doesn't appear to be valid for mysql.
You are looking for like:
where runHash like concat('%', key, '%')
You can put periods in the pattern as well, if those are important for your pattern matching.

mysql (5.1) > select * from database.table where not foo (on all fields)

I want to select everything in a table where the cell's value is not foo. I thought it would be something similar to WHERE NOT NULL, like SELECT * FROM database.table WHERE NOT 'foo';, but that just returned the column headers with no data.
edit: i want to do this without specifying a particular column (NOT 'foo' for all fields).
There's no way to do this without specifying the fields and conditions, as in
select * from table where
col1 != value and
col2 != value and
...
Depending on what you mean by
the cell's value is not foo
you may need to change and (none of the columns in a row match the condition) to or (at least one column does not match the condition).
As #Jim Garrison answers, you must specify the columns to compare your 'foo' value against. Otherwise you're testing a constant value itself, and that can be either false (zero) or true (nonzero).
SELECT * FROM database.table WHERE NOT 'foo' returns no data because 'foo' is a non-false constant value. Therefore NOT 'foo' is false for every row in the table, and so no rows match and the result is an empty set.
Jim gives one syntax for testing each column in turn. Here's another alternative syntax:
SELECT * FROM database.table WHERE 'foo' NOT IN (column1, column2, column3, ...)
However, I agree with the comment from #Mosty Mostacho. In a relational database, it's weird to test for a single value over many columns. Each column should be a different logical type of attribute, so it's uncommon to look for a similar value among many columns (not impossible, but uncommon).
It usually means you're using repeating groups of columns, which violates First Normal Form. Instead, you may need to create a child table, so that all the values you are searching are in a single column, over multiple rows.
It seems like MySQL is not sophisticated enough to do this. Instead I'll have to add an if-test in my php loop to check for foo and break if foo is present. I didn't want to do this as I have to get down into several nested loops before testing a cell's value…

Fix following id's when a row is deleted

I have a table with some rows, each row has a unique key. When a row is deleted from the table, all rows that are below this row should be 'moved up'. Is there some built in function in MySQL that does this or should I just do it with PHP or perhaps UPDATE table SET id=id-1 WHERE id > deletedid?
Using the last one seems a bit messy.
What would be the best way to do this?
Why do you want to do this? I know it's ugly to have holes in your unique ID sequence, but the downside of invalidating any references to IDs from outside the database is normally very much greater. The normal thing is to just accept the sequence won't be contiguous. If these represent a sequence, consider just sorting by the order rather than expecting the N'th value to have value N (any sort of iteration should provide its own index somewhere for this use).
If the value is one you set yourself, and you definitely want to keep it as having values from 1 to N (N="number of rows"), and you want to keep the sequence of values even if they're not in the order the rows were inserted, then "UPDATE table SET id=id-1 WHERE id > deletedid" is probably the best answer.
If the value is an auto_increment field, and you don't care which numbers go with with rows as long as each row has a number from 1 to N, you can alternatively do ALTER TABLE DROP COLUMN 'columnname' and then ALTER TABLE again to add the column again, and the database will regenerate the ids from 0. (Not necessarily in the same order, though it often is.)
There may be a way to renumber only the rows after that point, but (according to a quick google) it doesn't look like there's anything easier than what you're already planning.
First you have to ensure that the column is not a foreign-key for any other table.
Then you can try this (I am not 100% positive it will work):
DELETE FROM
MyTable
WHERE
id = deletedid;
UPDATE
table
SET
id=id-1
WHERE
id > deletedid
ORDER BY
id
As stated in mysql docs:
If the ORDER BY clause is specified,
the rows are updated in the order that
is specified.
and in this way you ensure uniqueness of the field.

MySQL : multiple column values as 1 comma-separated string?

Suppose I have a select query like :
SELECT * FROM tablename
and in the table are columns : field1, field2 and field3
Does anyone know if it's possible to get a resultset with only 1 row with 1 field, with comma separated values of the columns, like this :
"fieldvalue1, fieldvalue2, fieldvalue3"
Problem is that I don't know the column names of the table in advance...
Another problem is that a prepared statement is not the way to go, since all this should be done from within a trigger, and MySQL doesn't allow dynamic cursors/selects inside a trigger.
I have done some research and only came as far as GROUP_CONCATenating the column names correctly. But the problem is, that
SELECT (SELECT GROUP_CONCAT( cols.column_name) FROM (SELECT column_name FROM information_schema.columns WHERE table_name='test_table') as cols) FROM test_table
will return one and the same concatenated string containing the column names, once for each table row, instead of evaluating it as the column names for the outer select statement and returning the actual values.
From what I have read in all of the forums discussing this kind of question (and there were many), there is really no way of getting this to work without prepared statements.
I can only think of one other way to do this, and that would be having a dedicated column on each table, where you concatenate the individual column values on INSERT or UPDATE, so you can simply select this one field instead of the full set of fields.
Seems like you have 3 questions here:
Getting a resultset with 1 row, 1 field: MYSQL has a CONCAT_WS function that works like this:
SELECT CONCAT_WS(',',Field1,Field2,Field3)
That will return "Field1Value, Field2Value, Field3Value"
I'm not sure how you are going to get these column names. Do you need to get them from a sql statement, a string, etc. ? You can get the table names `SHOW COLUMNS FROM tablename'. The Field column will have the column names.
Triggers are available in mysql (added in 5.0.2 I think): http://dev.mysql.com/doc/refman/5.0/en/triggers.html
First, to find out the columns' names in advance, assuming that you have the table's name, you can get them as any other query:
SHOW COLUMNS FROM your_table
Once you have the names you can do:
SELECT CONCAT(field1,',',field2,',',field3) AS newField FROM your_table