Why ' ' was used in select statement - mysql

Came across this code today:
SELECT 'Overall' as Main,
wave,
country,
catg,
'' AS hw,
SUM(0) AS headwinds_sum
....
....
Can someone explain what ' ' in the above stands for?
Its not a typo as it is repeated #multiple instances.
Not a typo, no text was missed to add.

'' as hw
adds a column named hw to your select query of type varchar that contains empty strings.
Depending on how you process the resultset afterwards this can make sense.

The symbol is used to return an empty column in a result set. Users occasionally do this to match column counts in insert selects or when exporting data to Excel files and you want standard column names for capturing audit recommendations on data etc.

Related

Select rows in SQL partially matching an input input

I would like to select rows in my table (I'm using Google Sheet for that purpose) which content is included in the string.
For example, rows included in table called Jobportal, column Test:
How to find work
Work permit
Jobs
Temporary jobs
I want to select all the rows that contain any word of my input, so if I write "i'm looking for a job", I need to select rows Jobs and Temporary jobs. If I write "where is my work?", I need to select How to find work and Work permit.
I've tried this query, but it's returning wrong/unexpected results.
select * from Jobportal where 'im looking for a job' LIKE CONCAT('%',Test,'%');
You can use regular expressions. Assuming that what the user types does not have special characters:
where test regexp replace('im looking for a job', ' ', '|')
That said, for performance you might want to consider using full text search capabilities.

Composing dynamic SELECT Statement

Some advise on this issue:
select
concat(MySourceTable,',',sid,'X',gid,'X',qid) as MySourceFieldname
from
MySchemaTable
where
SomeCriteria;
Using the upper Statement I get a list (one column only) of fieldnames.
How can I transform this to be shown in horizontal (Fields next to each other) position, separated by comma
I want to produce a 'normal" SELECT-Statement for further use.
#amixdon
Input is :
select concat('shape_survey_990113',sid,'X',gid,'X',qid) as lsfield -- <=MySourceFieldname
from shape_questions -- <=MySchemaTable i.e. kind of dictionary
where question='result' -- <=SomeCriteria
and sid=990113
and language='en'
order by lsfield;
Result of Input looks like this (e.g.)
lsfields
---------
shape_survey_990113.990113X468X729,
shape_survey_990113.990113X469X733,
shape_survey_990113.990113X470X737,
....,
Explanation: sid, gid, qid are numeric contents taken from a table that is comparable to a dictionary. This is the source system which I want to select information from (cannot be changed). It is an opensource survey tool. Within this table all information for each survey is handled (numeric value 990113 is identifiying one special survey out of many. The concat above ist the final field name that holds the answers to the questions.
The 'shape_survey_990113' is the table to select the firlds from:
This select result should look like this an can be written to some variable
(e.g. SET #MyFields =...)
shape_survey_990113.990113X235X476, shape_survey_990113.990113X235X484, shape_survey_990113.990113X235X496
..to be used in the next step to make up a real select statement like:
concat('SELECT ', #MyFields, ' FROM shape_survey_991103;')
Unfortunately i cannot upload a screenshot fort this, not enough reputation....
There's nothing sacred, and quite a bit profane, about that CONCAT(). Lose it. And lose the quoted commas. Like so.
select
MySourceTable, sid, gid, qid
from
MySchemaTable
where
SomeCriteria;
Simple as can be.

sql server select where breaks with field containing apostrophe

I have set up a job to run reports and uses multiple tables with joins. I am joining two tables on a string field and if the field contains an apostrophe, it does not return any matches. This is weird and not sure why is is happening now and never before. I am perhaps not identifying the exact cause but will appreciate any help here:
Example query: "today's deals"
SET #TITLE = (SELECT MAX(B.DATEADDED) as 'td','',
(C.CLIENT + CHAR(10) + B.CLIENTKEY) as 'td','',
B.BADQ as 'td','',A.FULLQ as 'td','', B.BADERROR as 'td',
''
FROM BADQUERY AS B
LEFT JOIN QDATA AS A ON B.BADQ = A.QUERYT
LEFT JOIN Clients AS C ON C.clientKey = B.clientKey
WHERE DATEDIFF(minute,CAST(B.DATEADDED as datetime),GETDATE())<=420 AND
DAY(GETDATE()) = DAY(B.DATEADDED)
GROUP BY B.BADT,A.FULLQ, B.CLIENTKEY,C.CLIENT, B.BADERROR
FOR XML PATH ('tr'), ELEMENTS XSINIL)
For some reason A.FULLQ is being returned as NULL. When I do it separately with just a query the result set is also null but I know the matching record in QDATA as A is in the table. So if it is the query with apostrophe how can get the matching field or is sql server matching the data and something else is wrong.
If I try and match with a like it returns results but this is not accurate.
If B.BADQ and A.QUERYT don't exactly match, you won't get any records back. The fact that it works with a LIKE makes me wonder whether one of them has additional characters, either before or after the matching data (depending on how you set up the LIKE).
Michael Green is right, below, that trailing blanks by themselves don't prevent a match, but, depending on where your data originates, you might have some other character (such as an embedded CHAR(0) or a TAB character) that doesn't appear when you view the data in the record but which is enough to prevent the records from matching. You might use the CHECKSUM() function on the two strings to verify that they do represent the same data.
Another, similar possibility is that if there is a string of blanks in the values (something like "A, B, ' '") the number of blanks might be different between the two instances. They'd look the same in HTML (which it looks like you're generating) but they'd be different in reality and be enough to prevent a match.
Finally, the fact that you're generating XML and observing trouble with apostrophes made me think of this: if the content of an XML tag has an apostrophe, it will be converted to &apos;. That ought to affect only the output, not the functioning, of the query, but I don't know what your data actually looks like.

How to update Mysql row that has serialized data?

I have 2000 products with row that is using serialized data and I need to update specific string
this is the row name data
a:35:{s:11:"expire_days";s:3:"30d";s:12:"trial1_price";s:0:"";s:11:"trial1_days";s:0:"";s:12:"is_recurring";s:0:"";s:10:"start_date";s:0:"";s:5:"terms";s:24:"$150 for 1 Per license";s:12:"rebill_times";s:0:"";s:15:"paypal_currency";s:0:"";s:4:"##11";N;s:3:"url";s:0:"";s:8:"add_urls";s:0:"";s:4:"##12";N;s:5:"scope";s:0:"";s:5:"order";s:4:"1010";s:11:"price_group";s:1:"7";s:13:"renewal_group";s:2:"28";s:14:"need_agreement";s:0:"";s:13:"require_other";a:1:{i:0;s:0:"";}s:16:"prevent_if_other";N;s:4:"##13";N;s:19:"autoresponder_renew";s:0:"";s:16:"dont_mail_expire";s:0:"";s:13:"joomla_access";s:2:"36";s:10:"files_path";s:108:"products/Boxes8.zip|Box 8
products/Boxes9.zip|Box 9";s:14:"download_count";s:0:"";s:18:"download_unlimited";}
and only thing I need changed is
s:24:"$150 for 1 Per license";
any help is appreciated.
You should probably SELECT the row, make your changes, then UPDATE with the new value. The answer to this question may be helpful if you need to do this database side.
How to do a regular expression replace in MySQL?
If you want to replace the value of that single field with something else, you can use the following query:
UPDATE table SET col = CONCAT(
LEFT(col, LOCATE('s:24:"', col) + 5), -- up to and including the opening quote
'Now for free', -- new replacement text
SUBSTR(col, LOCATE('"', col, LOCATE('s:24:"', col)+6)) -- closing quote and everything after that
) WHERE col LIKE '%s:24:"$150 for 1 Per license"%'
Note that there is potential for trouble: if the value of one of your fields should end in 's:24:', then that combined with the closing quote would get misinterpreded as the location you're looking at. I consider this risk unlikely, but if you want to play it safe, you might want to check for that with an elaborate regular expression that can deal with quoted strings and escaped quotes.

MySQL sort by name

Is ir possible to sort a column alphabetically but ignoring certain words like e.g 'The'
e.g.
A normal query would return
string 1
string 3
string 4
the string 2
I would like to return
string 1
the string 2
string 3
string 4
Is this possible?
EDIT
Please note I am looking to replace multiple words like The, A, etc... Can this be done?
You can try
SELECT id, text FROM table ORDER BY TRIM(REPLACE(LOWER(text), 'the ', ''))
but note that it will be very slow for large datasets as it has to recompute the new string for every row.
IMO you're better off with a separate column with an index on it.
For multiple stopwords just keep nesting REPLACE calls. :)
This will replace all leading "The " as an example
SELECT *
FROM YourTable
ORDER BY REPLACE(Val,'The ', '')
Yes, it should be possible to use expressions with the ORDER-part:
SELECT * FROM yourTable ORDER BY REPLACE(yourField, "the ", "")
I have a music listing that is well over 75,000 records and I had encountered a similar situation. I wrote a PHP script that checked for all string that began with 'A ', 'An ' or 'The ' and truncated that part off the string. I also converted all uppercase letters to lowercase and stored that string in a new column. After setting an index on that column, I was done.
Obviously you display the initial column but sort by the newly-created indexed column. I get results in a second or so now.