I have table with this structure :
IdCity int
CityName nvarchar(20)
the data inside it is written in russian like :
+--------+----------------+
| IdCity | CityName |
+--------+----------------+
| 1 | Абакан |
| 2 | Азов |
| 3 | Александров |
| 4 | Алексин |
+--------+----------------+
I tried to make an easy view to retrieve the rows that have city name = Азов :
SELECT IdCity, CityName
FROM dbo.City
WHERE (CityName = 'Азов')
It gave me null although the record is in the table.
When I tried to add a row with an english name:cityname = abc, for example and edited the view to select cityname = 'abc', it worked fine.
so how to make the sql query select the russian inputs also?
It can be the encoding issue. make sure your CityName type is VARCHAR, not NVARCHAR. and try to run this script.
SELECT IdCity, CityName
FROM dbo.City
WHERE (CityName = N'Азов')
The problem is when creating the database from the beginning i had to choose the encoding language for the db, can't be changed after creating it though !
Related
I have a database table look like this
+======+===========+============+
| ID | user Name |user surname|
+======+===========+============+
| 100 | name | surname |
| 101 | name | surname |
| 102 | name | surname |
+===============================+
When i run this query which should show me no rows because there is no row with 101foo2 value :
SELECT * FROM tableName WHERE ID = '101foo2'
I am getting a result with same ID without the foo2 word
+======+===========+============+
| ID | user Name |user surname|
+======+===========+============+
| 101 | name | surname |
+===============================+
how it is showing the row with ID 101 if my query is ID = '101foo2'
You are mixing types. ID is an integer (or number). You are comparing it to a string. So, MySQL needs to decide what type to use for the comparison. What types gets used? Well, a string? No. A number. The string is converted to a number, using the leading digits. So, it becomes 101 and matches.
You should really only compare numbers to numbers, and strings to strings. You could try to write the code as:
SELECT * FROM tableName WHERE ID = 101foo2
However, you would get an error. Another possibility is to force the conversion to a string:
SELECT * FROM tableName WHERE CAST(ID as CHAR) = '101foo2'
My table structure is as follows,
ID Name Source
1 John first.jpg
2 Doe second.jpg
3 Mary third.jpg
4 Kurian four.jpg
I would like to update the "Source" by prepending with the host and primary key as follows
http://example.com/1/first.jpg
http://example.com/2/second.jpg
http://example.com/3/third.jpg
http://example.com/4/four.jpg
tried with CONCAT("http://example.com/"+id,Source) but fails with Truncated incorrect DOUBLE value:
Any suggestion will be greatly apreciated.
Try
UPDATE table_name
SET Source = CONCAT('http://example.com/', ID, '/', Source);
Result
| ID | Name | Source |
|----|--------|---------------------------------|
| 1 | john | http://example.com/1/first.jpg |
| 2 | Doe | http://example.com/2/second.jpg |
| 3 | Mary | http://example.com/3/third.jpg |
| 4 | Kurian | http://example.com/4/fourth.jpg |
checkout this
SELECT CONCAT("http://example.com/" , CONCAT(ID , CONCAT("/" , Source))) FROM table_name;
or simply
SELECT CONCAT("http://example.com/" ,ID , "/" , Source) FROM table_name;
You can iterate in sql but the easiest way is to create a php script in which you create an array with the rows of the table
Then use the dot si tax to concat ID with the domain name and the source, something like:
$newval = ‘{$table[“ID”]}’ .“/Domainname.com/” . ’{$table[“Source”]}’;
Where $table is a variable storing the associative array of the table
And then make a new query in which you override the source column for each row in the table array
Please try below code:
UPDATE table_Name
SET Source = concat(concat(CONCAT('http://example.com/',ID),'/'),source);
There is a table named as customer like this:
| Name | Age | Balance |
------------------------------
| Kevin | 25 | 150000 |
| Bob | 33 | 350000 |
| Anna | 27 | 200000 |
Simply, to select "Name" column we can use:
SELECT Name FROM customer
Now, I want to do that by using a variable like this:
SET #temp = 'Name'
SELECT #temp FROM customer
The result I get:
| #temp |
-----------
| Name |
The result I want is same like the normal select:
| Name |
----------
| Kevin |
| Bob |
| Anna |
I am expecting this will run the same like "SELECT Name From Customer", so it basically run the SELECT from a variable value.
I also use a function returned value to do the same thing, but I get the similar result. For example, there is function called CustName(Value):
SELECT CustName(A) // Return : 'Name'
FROM customer;
This will give me result:
| CustName(A) |
-----------------
| Name |
Is there any way that MySQL will run "Name" normally like when I basically write "Select Name from customer" ?
What you're saying you're looking for is dynamic sql.. it's generally not a fabulous idea as you're trying to vary a part of a query that the database wants to be fixed, for performance reasons. You'll also struggle to make use of your sql in a client app if it's expecting a string of a username, but then the user supplied 'birthday' as the thing to select and your client gets a date instead
If you're hell bent on doing it, this SO post gives more detail: How To have Dynamic SQL in MySQL Stored Procedure
I must ask you to consider though, that this is a broken solution you've devised, to some other problem. It might be better to post the other problem as solving it may prove more productive
I have the following query
SELECT id,title,image,address,serviceType FROM `ta` ORDER BY title LIMIT 0,20
It gives me results based on ordering of the titles.
The default ordering puts 'titles' starting with special characters first, then titles starting with numbers and then titles starting with alphabets.
But I want, first titles starting with numbers, then titles starting with alphabets and then titles starting with special characters.
Please help.
You can put case statement in your order by clause. Somthing like this:-
SELECT id, title, image, address, serviceType
FROM `ta`
ORDER BY CASE WHEN title LIKE '[0-9]%' THEN 1
WHEN title LIKE '[A-Z]%' THEN 2
ELSE 3 END
LIMIT 0,20
Hope this helps.
How to ask a question:
Hello, I have a table like this:
CREATE TABLE strings
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,string VARCHAR(12) UNIQUE NOT NULL
);
INSERT INTO strings (string) VALUES
('apple'),
('banana'),
('cherry'),
('*durian'),
('1eggplant'),
('10fig'),
('grapefruit');
SELECT * FROM strings;
+----+------------+
| id | string |
+----+------------+
| 4 | *durian |
| 6 | 10fig |
| 5 | 1eggplant |
| 1 | apple |
| 2 | banana |
| 3 | cherry |
| 7 | grapefruit |
+----+------------+
I would like a result set like this...
[PUT YOUR DESIRED OUTPUT HERE]
... where the rows are arranged by x, y z.
I tried this...
[PUT YOUR BEST EFFORT HERE]
... but that's not right because it's wrong in the following ways...
I've got a table in MySQL that looks roughly like:
value | count
-------------
Fred | 7
FRED | 1
Roger | 3
roger | 1
That is, it was created with string ops outside of MySQL, so the values are case- and trailing-whitespace-sensitive.
I want it to look like:
value | count
-------------
Fred | 8
Roger | 4
That is, managed by MySQL, with value a primary key. It's not important which one (of "Fred" or "FRED") is kept.
I know how to do this in code. I also know how to generate a list of problem values (with a self-join). But I'd like to come up with a SQL update/delete to migrate my table, and I can't think of anything.
If I knew that no pair of records had variants of one value, with the same count (like ("Fred",4) and ("FRED",4)), then I think I can do it with a self-join to copy the counts, and then an update to remove the zeros. But I have no such guarantee.
Is there something simple I'm missing, or is this one of those cases where you just write a short function outside of the database?
Thanks!
As an example of how to obtain the results you are looking for with a SQL query alone:
SELECT UPPER(value) AS name, SUM(count) AS qty FROM table GROUP BY name;
If you make a new table to hold the correct values, you INSERT the above query to populate the new table as so:
INSERT INTO newtable (SELECT UPPER(value) AS name, SUM(count) AS qty FROM table GROUP BY name);
Strangely, MySQL seems to do this for you. I just tested this in MySQL 5.1.47:
create table c (value varchar(10), count int);
insert into c values ('Fred',7), ('FRED',1), ('Roger',3), ('roger',1);
select * from c;
+-------+-------+
| value | count |
+-------+-------+
| Fred | 7 |
| FRED | 1 |
| Roger | 3 |
| roger | 1 |
+-------+-------+
select value, sum(count) from c group by value;
+-------+------------+
| value | sum(count) |
+-------+------------+
| Fred | 8 |
| Roger | 4 |
+-------+------------+
I was surprised to see MySQL transform the strings like that, and I'm not sure I can explain why it did that. I was expecting to have to get four distinct rows, and to have to use some string functions to map the values to a canonical form.