MySQL how to get a specific row from a table - mysql

I have a database with the fields: land, zone, domestic. And I want to get all this info out depending on what land the user chooses. So I figured I do this:
SELECT * FROM myDB WHERE land=Norway (Norway is a stored country).
But I get the error unknown column Norway. Is this because it cant find Norway or what seems to be the problem?

It looks like you aren't specifying a table name try something like this:
SELECT * FROM MyDB.MyTable
WHERE land = 'Norway'
Also note that string Norway is in single quotes

You missed the single quotes before and after the country name that you've used, as per my comment on the question earlier!
SELECT * FROM myDB WHERE land = 'Norway'
The above query works on the assumption that your table is actually named as myDB

You need quotes around literal
SELECT * FROM myDB.tablename WHERE land='Norway';
(and a proper tablename) otherwise your value is used like a column name and give your the error Unknown column Norway.

You are query using a where clause that confront a column with text. So to compare you have to tell to the dbms the value to match . Using only Norway it doesn't understand that you are asking for the row with Norway value. Using 'Norway' you are telling "hey dbms here you are your text to confront". I hope that I'm to help.

You can use ` marks for choosing columns/tables, so you should do something like
SELECT `column1`,`column2` FROM `tablename` WHERE `column`="value"

Related

Select a specific part of a value from a column in mariadb

i´m new in SQL and want to solve following case.
I have a table in a MariaDB named inventories. In this table are 2 columns: identifier and data.
I want to select and output a specific part in the column data, if the identifier is a specific one.
For example:
identifier
data
steam:xxxxxxxxxxxxxxx
...some data...,"Serial":"ZF3CJ8L1rrKjwP7nKTzb",...some more data...
The only part, that i want in the output is this: "Serial":"ZF3CJ8L1rrKjwP7nKTzb" but the part behind "Serial":" is a random value.
How can i solve this?
(i put a screenshot in here, table does not work for me in editorscreenshot of table in stackoverflow editor.)
You can try using the SubString function, which only returns a certain amount of character and starts a whatever character you want.
I got it!
SELECT (SUBSTRING(data, LOCATE('"Serial":"', DATA) + 0, 31)) FROM inventories where identifier = 'steam:xxxxxxxxxxxx';

SELECT odd syntaxes in database

Basically I have a large MySQL database table with a lot of city names, 90% of them are valid entries, but some of them are written in a ... not valid way.
For example the valid way is juste "CITYNAME" but some of them are like "(CITY NAME)(COUNTRY)" or just "(CITY NAME)" so I just wanna SELECT all the entries that are not written the valid way.
I don't know if that's specific enough don't hesitate to ask me for some precise elements.
And please help I have no idea how to build my SQL query.
CREATE TABLE cities(
name VARCHAR(50) NOT NULL PRIMARY KEY
);
INSERT INTO cities(name) VALUES ('ORLANDO');
INSERT INTO cities(name) VALUES ('(CHARLOTTE)');
INSERT INTO cities(name) VALUES ('(PHOENIX)(USA)');
INSERT INTO cities(name) VALUES ('AUSTIN(USA)');
INSERT INTO cities(name) VALUES ('TENNESSEE NASHVILLE');
So here are some examples of the different kinds of entries that I have to deal with.
I don't know how to describe the desired output, that'd just be a list of odd syntaxes, with or without the brackets.
The whole point is to delete those odd entries, but I have to SELECT them before doing so. And I also won't be the one deleting them, just gotta SELECT.
Possible solutions with output:
select * from cities where instr(name,')') or instr(name,'(');
(CHARLOTTE)
(PHOENIX)(USA)
This looks for anything which contains "(" or ")" anywhere.
Why wouldn't you try something like
Select * from cities where name like '(%';
If the problem is the parenthesis.. then everything that starts with a parenthesis will be selected
EDIT:
Select * from cities where name like '%(%' or name like '%)%';
It gives every line that contains a ( OR a )
EDIT2: some explanation
The character '%' replaces any string. So if you put like '%randomthing%', it will look for everything that look like anystring_randomthing_anystring. I hope i made this clear
You are using MySql, so you can do:
select *
from cities
where name not regexp '^[A-Z-]+$';
The not regexp '^[A-Z-]+$' means all the city names that don't respect the format "AAAA" or "AAAA-AAAAA" (where A represent an uppercase letter) is considered as invalid.

Search comma separated string using LIKE in mysql

I have a table with 3 columns something like below,
expert table
id - 1589
name - Jhonny
expert_in - 1,12,8 (Values similar like this)
The experts_in contains another table's foreign key
experts_in table
id - 1
expert_in - painting
I want search experts who are expert in some jobs while searching for experts
SELECT * FROM `experts` WHERE expert_in LIKE 1%
The above query brings all experts with 11,12,13...etc. I want only exact word. I know LIKE will bring all. Is there any way to achieve this without altering table. Thanks in advance.
You should use REGEXP. Try this query:
SELECT * FROM experts
WHERE expert_in REGEXP '[[:<:]]1[[:>:]]';
Output: See Live Demo on SQLFiddle
Note: You can adjust searching string based on your requirement above REGEXP is searching exact word.
if you can alter the data (not the table/schema) you should append and prepend another comma, so you can search with where col like "%,123,%", this will always fit on an exact value. Otherwise you have to use regex with something like ,?123,?

Query string with spaces

Is is possible to query string with spaces, a sentence for example, I have following query:
select id_group from groups where title = 'some title with spaces'
And I don't get any results although I have exact title in my database. I also tried with like but it's the same
Try this to get all titles with a white space.
select id_group from groups where title like '% %';
But you may find performance issues if there are many number of records and if the column is not indexed.
Yes, its possible, maybe try with double quotes? y just tried in my database
SELECT *
FROM `Faq`
WHERE faq_pregunta = "chao que te vaya bien"
and it works
There's nothing wrong with your query. The only explanation is that there just isn't any row that satisfies the condition.
It is possible. if you are using phpmyadmin you can press on "sql" and then try your code directly in the database. The problem i think is that you may use lack of ' or " or some other type of symbol. The problem can also be that you havent typed the exact same line. It is important to use capital on the same places and so on.

MySQL search within the last 5 characters in a column?

My user table has a column "name" which contains information like this:
Joe Lee
Angela White
I want to search for either first name or last name efficiently. First name is easy, I can do
SELECT * FROM user WHERE name LIKE "ABC%"
But for last name, if I do
SELECT * FROM user WHERE name LIKE "%ABC"
That would be extremely slow.
So I am thinking about counting the characters of the input, for example, "ABC" has 3 characters, and if I can search only the last three characters in name column, that would be great. So I want something like
SELECT * FROM user WHERE substring(name, end-3, end) LIKE "ABC%"
Is there anything in MySQL that can do this?
Thanks so much!
PS. I cannot do fulltext because our search engine doesn't support that.
The reason that
WHERE name LIKE '%ith'
is a slow way to look for 'John Smith' by last name is the same reason that
WHERE Right(name, InStr(name, ' ' )) LIKE 'smi%'
or any other expression on the column is slow. It defeats the use of the index for quick lookup and leaves the MySQL server doing a full table scan or full index scan.
If you were using Oracle (that is, if you worked for a formerly wealthy employer) you could use function indexes. As it is you have to add some extra columns or some other helping data to accelerate your search.
Your smartest move is to split your first and last names into separate columns. Several other people have pointed out good reasons for doing that.
If you can't do that you could try creating an extra column which contains the name string reversed, and create an index on that column. That column will have, for example, 'John Smith' stored as 'htimS nhoJ'. Then you can search as follows.
WHERE nameReversed LIKE CONCAT(REVERSE('ith'),'%')
This search will use the index and be decently fast. I've had good success with it.
You're close. In MySQL you should be able to use InStr(str, substr) and Right(str, index) to do the following:
SELECT * FROM user WHERE Right(name, InStr(name, " ")) LIKE "ABC%"
InStr(name, " ") returns the index of the Space character (you may have to play with the " " syntax). This index is then used in the Right() function to search for only the last name (basically; problems arise when you have multiple names, multiple spaces etc). LIKE "ABC%" would then search for a last name starting with ABC.
You cannot use a fixed index as names that are more than 3 or less than 3 characters long would not return properly as you suggest.
However, as Zane said, it's a much better practise to use seperate fields.
If it is a MyIsam table, you may use Free text search to do the same.
You can use the REGEXP operator:
SELECT * FROM user WHERE name REGEXP "ABC$"
http://dev.mysql.com/doc/refman/5.1/en/regexp.html