Modify All varchar elements in a MySQL column - mysql

In a MySQL database,
I have a table License with a few example rows as presented below:
ID | Key | Location
1 25 C:/Public/lics/1885-0001.lic
3 21 C:/Public/lics/1885-0006.lic
There are many such rows, which I would like to modify as given below:
ID | Key | Location
1 25 C:/Licenses/1885-0001.lic
3 21 C:/Licenses/1885-0006.lic
One of the columns from all the rows get modified. How do I update the table to make this change across all rows.

Judging from the docs I posted in my comment, I think you should do something like this:
UPDATE License SET Location = REPLACE(Location, 'C:/Public/lics', 'C:/Licenses');

UPDATE License
SET Value = REPLACE(Location, 'Public/lics', 'Licenses')

Related

Find existence of a record in MySQL table with input data as a list

I have a list of ids in text format as a comma separated value like so
("12345", "12346", "12347", etc, etc)
I would like to find their existence or non existence from a table say devices table which has a column called device ids (not primary key)
Ideally i would like to get a list which says if each item exists or not.
So far I have tried to get the query of those that exist and I have to manually find the non existing ones.
Is there a for loop I have to run on stored procedures or something like that. Please help.
Table structure
<pre>
| id | device_id | device_name |
+------+-----------------+---------------+
| 71 | 352701060409650 | 57X |
| 13 | 352701060409700 | 582 |
</pre>
You need to create a query with left join to the same table with 'IFNULL' condition. There already has been a post for this topic. Please check this out here.

Use table rows as columns on query select MySQL [duplicate]

This question already has answers here:
How can I return pivot table output in MySQL?
(10 answers)
Closed 8 years ago.
I need to make a query (not another table, I'm trying to avoid that please) joining 3 tables. I have one table that specifies ownership schemes, like this:
+--------------------------------------------+
|id_scheme | scheme_name | registration_date |
+--------------------------------------------+
and another table which contains medical equipment, these have one ownership scheme and a supplier id, and finally another table which contains the suppliers, so, I need a query that can tell me for every supplier, how many equipments i have, and how many of these equipments I have in each ownership scheme category. I don't know how to write a query that use the rows of a table (all my ownership schemes) as a query result columns. So, the query can return something like this:
+-----------------------------------+
|supplier|equipments|acquired|leased|
+-----------------------------------+
|Philips | 50 | 13 | 30 |
+-----------------------------------+
or like this:
+----------------------------------------------+
|supplier|equipments|acquired|leased|commodatum|
+----------------------------------------------+
|Philips | 50 | 13 | 30 | 7 |
+-----------------------------------+----------+
and so on.
Please, your help will be very appreciated.
EDIT
I put an example of what i want in the image, the equipment and suppliers tables of course have more columns, but i thinks thats all i need to build the query
I hope i understand your question. Heres me trying to give an answer with an example.
SELECT name,author,title from customerstable,classicstable
WHERE customerstable.isbn = classicstable.isbn;
This would display a table that has data from two tables (customerstable and classicstable) whenever they share an isbn.
Of course you would change your values with your data. But a more detailed structure would help for a better answer. maybe even the first few rows for each table you are trying to join in a query.
In your database the isbn could be replaced with ids that are the same throughout multiple tables.

MySQL calculate the difference of two columns into a Total column

Name |InitialScore | CurrentScore | Total
-----------------------------------------------
Bart | 145 | 95 | -50
Homer | 230 | 260 | 30
Lisa | 111 | 179 | 68
I have a table with the values above, what im trying to do is have the Total column find the difference between the initical score and the current Score from the table named mods.
i have tried multiple variations of this:
ALTER TABLE mods
ADD Total AS tphs + won PERSISTED
but i keep getting a "#1064 - You have an error in your SQL syntax;" My current MySQL is 5.1.70. Is there a way to accomplish a persisted column labeled Total so when i make changes to the CurrentScore column it automatically updates the total?
You want to use triggers. A trigger is a named database object that is associated with a table, and that activates when a particular event occurs for the table. This event can be an insert, update, or delete.
http://dev.mysql.com/doc/refman/5.0/en/triggers.html
In this case, you want to setup a trigger before the insert.
CREATE TRIGGER insert_total BEFORE INSERT ON mods
FOR EACH ROW SET Total = NEW.InitialScore + NEW.CurrentScore;
This will add InitialScore and TotalScore together and put it in the Total record. FYI: This trigger will work for both INSERT and LOAD DATA.
update MYTABLE set Total = CurrentScore - InitialScore where 1=1
It will update all the rows and if you want to update some specific rows than update the where clause accordingly.
But if you want it to update automatically then use Triggers .

MySQL Update Field with some prefix

i have table have prefixed with bok- and inv-
id | number
1 | bok-1
2 | inv-3
3 | bok-2
4 | inv-2
5 | inv-10
6 | bok-3
How can it sorted the field number prefixed with inv-?
Which in this case the result will be:
id | number
1 | bok-1
2 | inv-1
3 | bok-2
4 | inv-2
5 | inv-3
6 | bok-3
You could just use MySQL's SUBSTRING() function:
ORDER BY CAST(SUBSTRING(number, 5) AS SIGNED)
See it on sqlfiddle.
However, it would probably be better to store the prefix and integer parts in separate columns, if at all possible:
ALTER TABLE mytable
ADD COLUMN prefix ENUM('bok', 'inv'),
ADD COLUMN suffix INT;
UPDATE mytable SET
prefix = LEFT(number, 3),
suffix = SUBSTRING(number, 5);
ALTER TABLE mytable
DROP COLUMN number;
Basically you should redesign your database structure. Unfortunately no other options possible processing this efficiently since the database won't index on those dashes. So separate both in 2 fields is the most common practice. Otherwise you will run table scans on every order by clause.
Edit: In addition to the information from the discussion you had: https://chat.stackoverflow.com/rooms/13241/discussion-between-eggyal-and-gusdecool it is clear that this is a wrong design and the operation you are asking for should not be executed at all.
It would be both impossible to realize it without created a decent structure and to create a solution this way which would be legally ok.

mysql fast select query without reading all db

I have a large database with two tables: stat and total.
The example of the relation is the following:
STAT:
| ID | total event |
+--------+--------------+
| 7 | 2 |
| 8 | 1 |
TOTAL:
|ID | Event |
+---+--------------+
| 7 | "hello" |
| 7 | "everybody" |
| 8 | "hi" |
This is a very simplified version; also consider that STAT table could have 500K records, and for each STAT I can have about 200 TOTAL rows.
Currently, if I run a simple SELECT query in table TOTAL the system is terribly slow.
Could anyone help me with some advice for the creation of the TOTAL table? Is it possible to say to MySQL that the id column is already sorted so that there is no reason to scan all the rows till the end where, for example, id=7?
Add INDEX(ID) to your tables (both), if you did not already.
SELECT COUNT(*) FROM TOTAL WHERE ID=7 -> if ID is indexed, this will be fast.
You can add an index, and furthermore you can partition your table.
As per #ypercube's comment, tables are not stored in a sorted state, so one cannot "tell" this to the database. However you can add an index on tables to make them faster to search.
One important thing to check - it looks like TOTAL.ID is intended as a foreign key - if so, the table TOTAL should have a primary key called ID. Rename the existing column of that name to STAT_ID instead, so it is obvious what it is a foreign key for. Then add an index on STAT_ID.
Lastly, as a point of style, I recommend that you make your table and column names case-insensitive, and write them in lower-case. It makes it easier to read SQL when keywords are in upper case, and database objects are in lower.