parse rows based on columns - mysql

I have a column which stores row information in a concatenate format such as:
LOCATION_COLUMN
[Country]*[city]*[town]
I want to extract the country )before the first aster ix part of it, [Country], while eliminating the [city][town]. I could complete this using a scripting language such as php which has great parsing tools, but if I could I like to complete the task inside of mysql. Looking at documentation I can't find any parsing techniques (there seem to be some parsing plugins for mysql), but I would like to keep the database as it is as it is used for many other things. Native sql syntax such as the commands LIKE I do not believe as be used as you need to know the characters you are looking for, but in my case I am not looking for specific characters, just want to extract the country parts. Is this somehow possible?

Assuming you mean that the components are separated by asterisks, you can use:
select substring_index(location_column, '*', 1) as country

Related

How to count number of ids in a file

So I have a huge file containing hundreds of thousands lines. I want to know how many different sessions or ids it contains. I really thought it wouldn't be that hard to do, but I'm unable to find a way.
Sessions look like this:
"session":"1425654508277"
So there will be a few thousand lines with that session, then it will switch, not necessarily incrementing by one, at all, I don't know the pattern if there's one. So I just want to know how many sessions appear in the document, how many are different between each other (they SHOULD be consecutive but it's not a requirement just something I noticed).
Is there an easy way to do this? Only things I've found even remotely close are excel macros and scripts, which lead me to think I'm not asking the right questions. I also found this: Notepad++ incrementally replace but it does not help in my case.
Thanks in advance.
Consider using jq. You can extract session with [.session], then apply unique, then length.
https://stedolan.github.io/jq/manual/
I am no jq expert, and have not tested this, but it seems that the program
unique_by(.message) | length
might give you what you want.
According to your profile, you know JavaScript, so you can use that:
Load the file.
Look for session. (If this is JSON, this could be as simple as myJson['session'].)
Keyed on session value, add to a map, e.g. myCounts[sessionValue] = doesNotMatter.
Count the number of keys in the map.
There are easier ways, like torazaburo's suggestion to use cat data | uniq | wc, but it doesn't sound like you want to learn Unix, so you may as well practice your JavaScript (I do this myself when learning programming languages: use it for everything).
You won't be able to achieve this with notepad++, but you can use a linux command shell command, i.e.:
cat sessions.txt | uniq | wc
Adding to my own question, if you manage to get the strings you want separated by columns in Excel, Excel has an option to Filter which automatically gives you the different values to filter a column by.
This means, applied to my case, if I get the key-value ("session":"idSession", the 100000 values each in a row), all of it in one column, filter, count manually, I get the number of different values.
Didn't get to try the wc/unix option because I found this while trying to apply the other method

Obtaining the average of one field with comma delimited values (InfoPath)

I have a field where the user enters multiple values each separated by a comma eg "1.8, 2, 3".
I want to find the average of those values. Is there a way to utilise avg() to accommodate for stripping the comma and producing the mean?
Unfortunately you can't do that with the built in InfoPath functions (there is no traditional split method for strings).
If you are willing to tackle it - using managed code behind the form will very easily solve your problem (only about 4 lines of code). Basic math and string manipulation should not impose any security restrictions on the form. However you will have to setup for code behind which is easy but can seem like somewhat of a hassle the first time you try it. There are good MSDN articles on how to go about that.
Alternatively, if you can change your data entry from comma separated to a repeating table you can use the built in avg() function.

Store Miscellaneous Data in DB Table Row

Let's assume I need to store some data of unknown amount within a database table. I don't want to create extra tables, because this will take more time to get the data. The amount of data can be different.
My initial thought was to store data in a key1=value1;key2=value2;key3=value3 format, but the problem here is that some value can contain ; in its body. What is the best separator in this case? What other methods can I use to be able to store various data in a single row?
The example content of the row is like data=2012-05-14 20:07:45;text=This is a comment, but what if I contain a semicolon?;last_id=123456 from which I can then get through PHP an array with corresponding keys and values after correctly exploding row text with a seperator.
First of all: You never ever store more than one information in only one field, if you need to access them separately or search by one of them. This has been discussed here quite a few times.
Assuming you allwas want to access the complete collection of information at once, I recommend to use the native serialization format of your development environment: e.g. if it is PHP, use serialze().
If it is cross-plattform, JSON might be a way to go: Good JSON encoding/decoding libraries exist for something like all environments out there. The same is true for XML, but int his context the textual overhead of XML is going to bite a bit.
On a sidenote: Are you sure, that storing the data in additional tables is slower? You might want to benchmark that before finally deciding.
Edit:
After reading, that you use PHP: If you don't want to put it in a table, stick with serialize() / unserialize() and a MEDIUMTEXT field, this works perfectly, I do it all the time.
EAV (cringe) is probably the best way to store arbitrary values like you want, but it sounds like you're firmly against additional tables for whatever reason. In light of that, you could just save the result of json_encode in the table. When you read it back, just json_decode to get it back into an array.
Keep in mind that if you ever need to search for anything in this field, you're going to have to use a SQL LIKE. If you never need to search this field or join it to anything, I suppose it's OK, but if you do, you've totally thrown performance out the window.
it can be the quotes who separate them .
key1='value1';key2='value2';key3='value3'
if not like that , give your sql example and we can see how to do it.

How to select XML attribute - Converting MSSQL to MySQL

I need to convert the MSSQL query below to MySQL. The query selects the value of the "id" attribute out of the "page" element in the xpath. I would expect the MySQL query to return 3 rows with the values 1,2,3 respectively. Does anybody know how to do this in MySQL? Please let me know.
DECLARE #InputXML as xml
SET #InputXML = '<pages><page id="1"/><page id="2"/><page id="3"/></pages>'
SELECT
Node.value('#id', 'Bigint') AS ID
FROM #InputXML.nodes('/pages/page') TempXML (Node))
EDIT: forget what I said earlier.
Courtesy of Dave, you probably want to look at this:
http://dev.mysql.com/doc/refman/5.1/en/xml-functions.html
(previous answer left in place for reference):
MySQL doesn't do XML. So the short answer is: "you can't".
The long answer is, depending on the context this runs in, you have a few options.
Normalize your data into a proper SQL schema, instead of relying on XML inside SQL.
Pull the XML from the database as plain strings, and do the XML processing in your logic (PHP, C#, VB, whatever it is you're using). Unfortunately, this means you can't filter your data by xpath expressions on the database, so you'll have to pull in all candidate rows and then discard the ones you don't want.
Ditch the database altogether (at least for this part) and just store XML files. If storing and retrieving XML files is all your application does, and you're not adding any meta-information, then the database has little to no benefit.

What is the equivalent of "FOR XML AUTO" in MySQL? (for having an XML string that represents a row)

In SQL Server I frequently use "FOR XML AUTO", which allows something like "SELECT id FROM car FOR XML AUTO" to return "<car><id>1</id></car><car><id>2</id></car>". Is there a way to do this in MySQL?
What I want is to be able to store this XML in another table so I can have complete log of changes made in the database. The history table will be populated by triggers, and each row will be a denormalized representation of the row that changed; storing XML seems like a great way to change the definition of a table and not worry about messing up the log history. So what I need is a simple and straightforward way to have an string that can be inserted as a data value, and this is string must be the XML description of the row.
Thank you very much!
Regards,
Pedro
See http://www.mysqludf.org/lib_mysqludf_xql/index.php for the lib_mysqludf_xql library from the MySQL UDF Repository.
The library provides a set of functions that generate XML output similar to SQL/XML as seen in Oracle/SQL Server. There are download/build/install instructions on the page along with a description of the library and the various functions it provides.
These should be sufficient to be used from within a trigger as you were planning.
-Dipin
See http://eriksdiary.blogspot.com/2007/07/xml-output-from-mysql.html
for a way to do this in mysql. It's a workaround... and not terribly straightforward but afaik mysql doesn't have an easy way to do this
Also see: http://www.redips.net/php/from-mysql-to-xml/ if you're planning on doing this with php (much simpler method)
There is no exactly same clause, but you can easily emulate it using:
SELECT GROUP_CONCAT(CONCAT('<id>', id, '</id>' SEPARATOR ''))
FROM car