I want to know why
$amzius_sql = "SELECT DISTINCT `Age` , SUBSTRING( Age, LOCATE( ' ', Age ) ) AS `AgePrefix` , SUBSTRING_INDEX( Age, ' ', 1 ) AS `AgeValue` FROM `suoPage` ORDER BY `AgePrefix` , `AgeValue` LIMIT 0 , 30";
$amzius_res = mysql_query($amzius_sql);
works flawlessly and this:
$amzius_sql = 'SELECT DISTINCT `Age` , SUBSTRING( Age, LOCATE( " ", Age ) ) AS `AgePrefix` , SUBSTRING_INDEX( Age, " ", 1 ) AS `AgeValue` FROM `suoPage` ORDER BY `AgePrefix` , `AgeValue` LIMIT 0 , 30';
$amzius_res = mysql_query($amzius_sql);
produces an error:
Unknown column ' ' in 'field list'
The only difference is quotes. If both queries are queried in phpMyAdmin - both work, but only the first one works if queried by mysql_query() in PHP
Here is the explaination:
The ANSI_QUOTES mode causes the server to interpret double-quoted
strings as identifiers. Consequently, when this mode is enabled,
string literals must be enclosed within single quotation marks. They
cannot be enclosed within double quotation marks.
Link
Related
I am getting the error "Incorrect parameters in the call to native function 'CONCAT': on the query below:
SELECT
*,
GROUP_CONCAT(
CASE
WHEN `REASONORINSTRUCTIONCODE` = 'R'
THEN CONCAT(
"name-",
`USERWHOENTEREDTHISLINE`,
",reason-",
RTRIM(
`REASONSORSHIPPINGINSTRUCTIONS`
)
END,
", "
)
) AS reason,
GROUP_CONCAT(
CASE
WHEN [ `REASONORINSTRUCTIONCODE` ] = 'S'
THEN CONCAT(
"name-",
`USERWHOENTEREDTHISLINE`,
",shipping instruction-",
RTRIM(
`REASONSORSHIPPINGINSTRUCTIONS`
)
END,
", "
)
) AS shipping instruction
FROM
TABLE
GROUP BY `PICKUP_NO`
You have several issues with your query. First, you're not closing your CONCAT with an end ), next your AS shipping instruction cannot contain a space. Next, you have [REASONORINSTRUCTIONCODE], remove the []
Take a look at the formatted query below:
SELECT
*,
GROUP_CONCAT(
CASE
WHEN `REASONORINSTRUCTIONCODE` = 'R'
THEN CONCAT(
"name-",
`USERWHOENTEREDTHISLINE`,
",reason-",
RTRIM(
`REASONSORSHIPPINGINSTRUCTIONS`
))
END,
", "
)
AS reason,
GROUP_CONCAT(
CASE
WHEN `REASONORINSTRUCTIONCODE` = 'S'
THEN CONCAT(
"name-",
`USERWHOENTEREDTHISLINE`,
",shipping instruction-",
RTRIM(
`REASONSORSHIPPINGINSTRUCTIONS`
))
END,
", "
)
AS shipping_instruction
FROM
`TABLE`
GROUP BY `PICKUP_NO`
I want to get nth word from a column I'm using a code line and it works for me but there is an issue, for example:
First line is: "the Nth word from database"
Second line is: "return the Nth word from database and more words"
When I search for 6th word 'database' it returns my first line and second line but I don't want to get my first line because it has only 5 words.
thank you all
My code line:
SELECT *,
SUBSTRING_INDEX(SUBSTRING_INDEX(`Text`, ' ', 6), ' ', -1) as Nth
FROM `tbl_name`
Having six words in you sentence means that you have to have at least five spaces, adding simlpe condition will resolve your problem:
select *,
case when length(`text`) - length(replace(`text`, ' ', '')) >= 5 then
substring_index(replace(`text`, substring_index(`text`, ' ', 5) , ''), ' ', 2)
else null end Nth
from `tbl_name`
Also I changed your query, because it didn't take into account that you might not have 6th space (exactly six words).
Demo
Or even more concicse:
select *,
substring_index(substring_index(`text`, ' ', 5 - (length(`text`) - length(replace(sentence`text` ' ', ''))) - 1), ' ', 1)
from `tbl_name`
Another demo.
You should update your query with where clause, in where you can count the number of words by the following query.
SELECT *, SUBSTRING_INDEX(SUBSTRING_INDEX(`Text`, ' ', 6), ' ', -1) as Nth
FROM `tbl_name`
where (COUNT(column1) - LENGTH(replace(column1, ' ', '')) > 5
You should have to take count of spaces or whichever string which you want to take & then need to apply having-clause on that count.
SELECT
* ,
SUBSTRING_INDEX( SUBSTRING_INDEX( `text` , ' ', 6 ) , ' ', -1 ) AS Nth,
ROUND( ( LENGTH( `text` ) - LENGTH( REPLACE( `text` , " ", "" ) ) ) / LENGTH( " " ) ) AS countq
FROM `xp_test`
HAVING
countq >= 5
I have a vendors table in my database that am experimenting with, as shown below
And when i run the sql command below
SELECT vendor_name
FROM vendors
ORDER BY vendor_name
LIMIT 10
I get the output below
My issue is am trying to extract the second word from each vendor_name and when the second word doesn't exist it's supposed to return a blank cell.
And below is the sql query i have written to do just that
SELECT vendor_name,
SUBSTRING(
SUBSTRING( vendor_name, LOCATE(' ', vendor_name) + 1),
1,
LOCATE( ' ', SUBSTRING( vendor_name, LOCATE(' ', vendor_name) + 1) ) - 1
) AS second_word
FROM vendors
ORDER BY vendor_name
LIMIT 10
And here is the output of that sql query
If you notice from the output above, when the words in the vendor_name are more than two, it returns the second word just fine and when the vendor_name contains one word it returns a blank cell as expected.
Problem comes when the vendor_name contains exactly two words, instead of returning the second word it is returning a blank cell for example in the case of American Express and ASC Signs.
How can i better improve my query so that even when the vendor_name does contain two words, it does return the second word instead of a blank cell?
Thank you.
That's because there is no space after the second word, if the text ends there, the locate() has no space to find.
Quick hack: Add a space at the end.
LOCATE( ' ', CONCAT(SUBSTRING( vendor_name, LOCATE(' ', vendor_name) + 1), ' ') ) - 1
SELECT vendor_name , substr(vendor_name , instr(vendor_name, " ") ,
case when LOCATE (' ', vendor_name,instr(vendor_name, " ") ) > 0 then LOCATE (' ',
vendor_name,instr(vendor_name, " ") ) else CHAR_LENGTH (vendor_name) end )
from vendors ;
I took tips from both #stick bit and #kiran gadhe and i came up with this sql query and it's working just fine
SELECT vendor_name,
CASE
WHEN INSTR( vendor_name, ' ' ) = 0
THEN
''
ELSE
SUBSTRING(
SUBSTRING( vendor_name, LOCATE(' ', vendor_name) + 1),
1,
LOCATE( ' ', CONCAT(SUBSTRING( vendor_name, LOCATE(' ', vendor_name) + 1), ' ') ) - 1
)
END AS second_word
FROM vendors
ORDER BY vendor_name
LIMIT 10
I discovered a couple of hours ago that CASE WHEN doesn't work on access queries. I have a complex CONCAT on my SELECT statement but it is written in MySQL:
SELECT CONCAT(
'www.mywebsite.com/catalogsearchtool?title='
, REPLACE(tblTitles.Content_Name, '&', '%26')
, '&contributor=ln:'
, CASE WHEN LOCATE('; ',tblTitles.Contributors) > 0
THEN REPLACE(REPLACE(REPLACE(
CASE WHEN RIGHT(TRIM(tblTitles.Contributors), 1) = ',' THEN LEFT(TRIM(tblTitles.Contributors), CHAR_LENGTH(TRIM(tblTitles.Contributors)) - 1) ELSE TRIM(tblTitles.Contributors) END
, '&', '%26'), '; ', ',rl:Author&contributor=ln:'), ', ', ',fn:')
ELSE REPLACE(REPLACE(
CASE WHEN RIGHT(TRIM(tblTitles.Contributors), 1) = ',' THEN LEFT(TRIM(tblTitles.Contributors), CHAR_LENGTH(TRIM(tblTitles.Contributors)) - 1) ELSE TRIM(tblTitles.Contributors) END
, '&', '%26'), ', ', ',fn:')
END
, ',rl:Author&language='
, LOWER(tblTitles.language)
) AS 'link'
Basically what that does is that it generates a link based on names that can be found in a column called Contributors. That column has the following order:
last_name, first_name
or
last_name, first_name; last_name, first_name
For reasons out of my control, there can be many names in that column. So for the query above a couple of results would be (for 2 names):
www.mywebsite.com/catalogsearchtool?title=tile123&contributor=ln:smith,fn:john,rl:Author&contributor=ln:smith,fn:jane,rl:Author&language=english
And (for 1 name):
www.mywebsite.com/catalogsearchtool?title=title5678&contributor=ln:smith,fn:john,rl:Author&language=english
Can anyone please help me translating this logic to Access SQL?
I have spent several hours to solve this problem, but I can't solve it.
One of column contain value like bellow, and the value contain some blank spaces.
光学传感器 - 光电二极管
I want select all the value equal to "光学传感器 - 光电二极管". But the blank space may be ASCII encoding('\x0a'), or utf8 encoding('\xa0'), So when I execute SQL string LIKE this, will return None.
SELECT *
FROM `icbase_icattrvalue`
WHERE `value` LIKE '光学传感器 - 光电二极管'
I have try SQL like this:
SELECT *
FROM `icbase_icattrvalue`
WHERE REPLACE( `value` , '\xa0', ' ' ) LIKE REPLACE( '光学传感器 - 光电二极管', '\xa0', ' ' )
LIMIT 0 , 30
or
SELECT *
FROM `icbase_icattrvalue`
WHERE REPLACE(`value`, CONVERT(char(160) USING utf8), ' ')
LIKE REPLACE('光学传感器 - 电二极管', CONVERT(char(160) USING utf8), ' ')
LIMIT 0 , 30
or some similar SQL string, But they can't help, they all return None.
What should I to , to select value equal to a string, but ignore such space encoding issue.
I'm sorry for my poor English.
As Alexander Gelbukh says in comment i also think that this is due to the incorrect spacing in ur code..
SELECT *
FROM `icbase_icattrvalue`
WHERE REPLACE( `value` , '\xa0', ' ' ) LIKE REPLACE( '光学传感器 - 光电二极管', '\xa0', ' ' )
LIMIT 0 , 30
Actually this is the right answer, I should replace c2a0, not a0.
NON BREAK SPACE in utf-8 is c2a0
And in python unicode is a0
I get confused with it, Thanks everybody.
SELECT * FROM `icbase_icattrvalue`
WHERE id = 197193 and
REPLACE( `value` , UNHEX('c2a0'), ' ' ) = REPLACE( '光学传感器 - 光电二极管', UNHEX('c2a0'), ' ' );