Insert ID in column with values separated by comma - mysql

I have a column that lists all the units ids from a company which are allowed to see a specific document.
Like this:
And those ids are selected here ('Selecionar todas' mean 'Select All'):
The problem is when I add a new Unit, it come unselected and it will be a headache to have to enter in all of the documents that are supposed to show to all units and change it to be selected.
Is there a way to insert the new Unit's id into the 'idsunidades' column where there are all the other units selected ?
Sorry for the bad English.

To implement a select all option you should use a flag column all_selected(true or false) or you can do it like this:
To select the rows where All units are selected should have the longest value of idsunidades, you can select them like this
SELECT #maxLength:=LENGTH(idsunidades)
ORDER BY LENGTH(idsunidades )
DESC LIMIT 1
You can use CONCAT() to append the new id to the old column data
To add the unit 99 to the column idsunidades to all the rows with all units you can do this
UPDATE table set idsunidades=CONCAT(idsunidades, ',99')
WHERE LENGTH(idsunidades) = (SELECT LENGTH(idsunidades)
ORDER BY LENGTH(idsunidades )
DESC LIMIT 1)

Related

SQL command for the last value. Additionally with a search ID

I would like to search my database via SQL command for the last value and for a given ID.
The ID works so well:
SELECT * FROM 'tem2' WHERE macid = '*'.
Unfortunately, if I add LAST ( SELECT LAST * FROM...) it does not work anymore.
SQL tables represent unordered sets. There is no "last" value unless a column specifies the ordering. Let me assume you have one.
Use order by and limit:
SELECT *
FROM tem2
WHERE macid = '*'
ORDER BY <ordering col>
LIMIT 1;
The ordering column would be a creation date or auto-incremented id.

SQL SELECT everthing with value 5 but not specify from which column

I have tried to select something with SQL, and I've a problem with it.
What I want:
SQL SELECT * FROM table WHERE ? = '5';
Select everything which = 5, BUT not specify from which column.
Example:
From this ""database"", you should receive the 1st and the last row.
Is that possible?
You have to list the columns but you can use in. The where clause looks like:
where 5 in (price, height)
Note: This assumes that the columns have the same type. You could get type conversion errors if they are not.
Also, given the names of the column and the data, I assume that the columns are stored as numbers. Hence, I dropped the single quotes around 5. If they are really strings, then use the single quotes.
you need to add a condition to your query with or keyword so if any of them match the row will be shown as a result
SELECT * FROM tablename WHERE price =5 or height= 5
better you list your columns by name instead of using * after SELECT

Delete all records in a table after a certain string in a row

I am importing a table from CSV to MYSQL on a regular basis. I need to clean the table by removing all rows after a given string in the row. Each import could have different amount of records, so deleting after a set row ID will not work because the string may be on a different row next time, therefore it needs to pick out the text.
Anyone know how this is done please?
An example below shows the different rows and text. So I need to delete all rows after 'Text d'.
Text a
Text x
Text k
Text d
Text a
Text u
Assuming you have an ID column and the order of the ID values are the same as the order of "delete every row after 'Text d'", you can read the ID of that specific row first:
SELECT
id
FROM
yourTable
Where
yourColumn = 'Text d'
Then you can use the Id for a second query to delete all the following rows
DELETE FROM
yourTable
WHERE
id > theSelectedIdValue
You might be able to use it in one query with something like this:
DELETE FROM
yourTable
WHERE
id > (SELECT
id
FROM
yourTable
WHERE
yourColumn = 'Text d')
However I'm not sure if this query works in MySQL.

data ordering issue in mysql for a particular column

I have a table in which there is column name as mappedcloumnname and fieldname and my fieldcolumn contains address1, address2, city, state, customerid, country and mappedcolumn contanins c1-c20. I wrote a query to sort my data based on mappedcolumn name but the order what am getting is wrong one
SELECT * FROM customermetadata
WHERE OrgID = in_orgid
ORDER BY MappedColumnName;
You can give it a try:
SELECT
*
FROM
customermetadata
WHERE
OrgID = in_orgid
ORDER BY CAST(SUBSTRING(MappedColumnName FROM 2) AS UNSIGNED);
Note:
Here I've extracted the number from the MappedColumnName and sort the records based the extracted numbers.
I've created a demo where the table contains only two columns (id and col).
col column contains value like c1,c2,....
See demo if you order by col only.
See demo if you order by extracting number from col.
If your column MappedColumnName contains value from c1-c20, then it will be treated as String(VarChar) in SQL and data will not be sorted properly.
You should try in this way,
select * from customermetadata
where OrgID=in_orgid
Order By CAST(SUBSTR(MappedColumnName FROM 2 FOR LENGTH(MappedColumnName)) AS UNSIGNED); ;
NOTE: A longer number is also a bigger number for numbers of the same length, you can make a textual comparison, because '0' < '1' .... < '9' and it will short Alpha-numeric order.

SELECT in mysql using column number instead of name

Is there any way to do something like :
SELECT * FROM TABLE WHERE COLUMN_NUMBER = 1;
?
No, you can't. Column order doesn't really matter in MySQL. See the below question for more details.
mysql - selecting values from a table given column number
If your table has a column named COLUMN_NUMBER and you want to retrieve rows from the table where that column contains a value of '1', that query should do the trick.
I suspect that what you are trying to do is reference an expression in the select list with an alias. And that is not supported. An expression in the WHERE clause that references a column must reference the column by name.
We can play some tricks with inline views, to give an alias to an expression, but this is not efficient in terms of WHERE predicates, because of the way MySQL materializes a derived table. And, in that case, its a name given to the column in the inline view that has to be referenced in the outer query.
How I did it:
I'm trying to take (last 3 values of) column number 4 in sometable.
set #mydb=(SELECT DATABASE());
set #mycol=(select COLUMN_NAME from information_schema.columns where
table_schema=#mydb and table_name='sometable' and ordinal_position = 4);
SELECT Date,#mycol FROM sometable ORDER BY Date DESC LIMIT 3;
Of course, if Database name is known, first line could by whiped and #mydb replaced by real database name.
You can do this trick
Example:
$query="select * from employee";
$result=mysql_query($query);
$meta=mysql_fetch_field($result,0) // 0 is first field in table , 1 is second one ,,, etc
$theNameofFirstField=$meta->name; // this well return first field name in table
// now you can use it in other query
$seconQuery="select $theNameofFirstField from employee";