selecting columns based on value in single column - mysql

I am complete newbie when it comes to MySQL. I have done some searching around here and elsewhere, but haven't been able to work out something that I imagine is very simple.
I have an email program that imports fields/columns from a MySQL database for bulk emails.
I am wanting to only import information for users that have a particular value in a particular column in a table.
To import all users I would normally use:
SELECT firstname, email FROM users
I have tried amending this to:
SELECT firstname, email FROM users WHERE group = "test"
where group is the name of the column that I am trying to test against, and test is the value I am searching for. I think this might be close, but it brings up an error.
Could someone put me straight?

I think your problem is that group is a keyword in MySQL. You can use
SELECT firstname, email FROM users WHERE `group` = "test"
Use back ticks to quote field names.

Related

Storing 'array' in SQL column and retrieving all rows WHERE certain element is in said array?

I currently have an SQL table listing all the different conversations between the users of a messaging app. This table contains a column titled participantIDs which contains the user ID of each of the (two) parties in the conversation. These values are separated by a comma, for example 19,25 would denote that the conversation was between user 19 and user 25.
Now, I am trying to work out how to retrieve the relevant conversations when a user looks at their messages inbox. To do this, I expect I will need to use a line of code such as
SELECT * FROM `convos` WHERE `participantIDs` LIKE (*contains user id*);
What would be the correct way to write this query?
instead of having one column separated by a coma ,i am suggesting you a simple way, you could create a table called "Personne" with a schema (id, idSender, idRecipient, messages) , and to select all message bbetween perssonne 1 and perssone 2 , you use this request
select messages from Personne where idSender = '1' and idRecipient = '2';
in this way you will respect the first normal form as explicated here Normalization of Database
This is a really, really bad way to do it. As a.slimane says, normalize your database!...
The gist of it is that:
select messages from Personne where idSender = '1' and idRecipient = '2';
...will be fast, since you will create an index on (idSender, idRecipient) which will allow to find the rows with a direct index lookup.
However, there is no way to index a search on "part of a column contains a value". This means MySQL will have to scan many rows (potentially the whole table if you really insist) and it will be terribly slow.
Well, there is one way to index search on "part of a column contains a value": FULLTEXT. But this does not apply here.

MySQL Command what does a point mean?

I'm a newbie in mysql and have to write a implemention for a custom mysql asp.net identity storage.
I follow this tutorial and the first steps are done.
https://learn.microsoft.com/en-us/aspnet/identity/overview/extensibility/implementing-a-custom-mysql-aspnet-identity-storage-provider
Now i have the follow mysql command:
"Select Roles.Name from UserRoles, Roles where UserRoles.UserId = #userId and UserRoles.RoleId = Roles.Id"
My problem is now that i dont know how the table have to look for this request?
I would say:
Tablename : Roles
Select: Roles and Name? or is it a name?
same with UserRoles.UserID and UserRoles.RoleId
What does the point mean?
Thanks a lot
You question is quite unclear, however, if I understood correctly, you can't figure out clearly how the database schema you are using is structured and what you'll get from this query.
The query you have written SELECTs the data field called Name from the table called Roles. In order to do this, the query uses data coming from two tables: one is the Roles table itself, the other is called UserRoles.
It will extract Names data from the Roles table only for the Roles entries that have the Id field matching with the RoleId field of the entries in the UserRoles table that have the UserId equal to the given #UserId.
In other words, this SELECT query in will give you as a result a list of Names coming from the entries in the Roles table which match the given conditional check, which is what is written after the where SQL condition: where UserRoles.UserId = #userId and UserRoles.RoleId = Roles.Id.
Finally, the point "." in SQL queries is used to disambiguate between fields (or columns, if you want to call it so) with same name but coming from different tables. It is quite common that all the tables have an Id field, for example. You can identify the correct Id field in your database by writing Table1.Id, Table2.Id, and so on. Even if you don't have naming conflicts in your tables columns, adding the table name can be very good for code readability.
Edit:
As other users correctly pointed out in the comments to your question, you should also have a look to what an SQL JOIN operation is. Since you are searching data using information coming from different tables, you are actually doing an implicit JOIN on those tables.

mySQL randomly update column data from existing data

Does anyone know if it is possible or a way to update columns and randomly change the text around or obfuscate it?
I want to batch update email addresses to something random #example.com for my users table.
I have a users table which contains
(id (unique), firstname, email_address (unique)
So thinking
id.firstname#example.com // (e.g 2012.jane#exmaple.com
I know this can easily be done with PHP, but does anyone know a mySQL function that can do this?
This worked.
update users set email = Concat(id,'.',firstname,'#exampe.com')

Count the number of times keywords (in a table) appear in a field in another table

I will simplify my problem in order to explain it.
I have a table which contains text messages posted by users and another table which contains keywords.
I want to display, for each user, the number of times keywords are found in text messages.
I don't want the result to display a keyword if it's not found in text messages.
I wan't it to be case INSENSITIVE. All keywords are lowered but in messages, you can find lower & upper chars.
Because I'm not sure that my explanation is clear enough, here comes the SQLFiddle.
http://sqlfiddle.com/#!2/c402a
Hope anyone can help me.
I found what I was looking for. It wasn't easy for me but here is my query :
SELECT t_msg.msg_usr,
t_list.list_word,
count(t_list.list_word),
t_msg.msg_text
FROM t_msg
INNER JOIN t_list
ON LOWER(t_msg.msg_text) LIKE CONCAT("%", t_list.list_word, "%")
GROUP BY t_msg.msg_usr, t_list.list_word;
The SQLFiddle is there : http://sqlfiddle.com/#!2/ba052/8
The recommendation would be to not try solving this with a query. It's possible to write a query that will do it, such query will scan the messages table for each keyword separately, and produce a count (or a row that you can group by), but this won't scale, or be reliable in sense of language search.
Here is what you might want to do:
Create a table to map (user_id, keyword_id) to a count of this keyword in messages of this user. Let's call it t_keyword_count.
Each time you receive a message, before you save the message into the database, search it for all the keywords you care about (using whatever good text search libraries that account for misspellings, etc.). You should know the (user_id) for this message.
You will, at that point, be ready to add the message to the database, and will have an array of (keyword_id) with keywords that this message will have.
In a transaction, insert the message into the t_msg table, and run update/insert for (user_id,keyword_id) to have value=value+1 (or +n, if you need to count the same keyword more than once in the same message) for the t_keyword_count table.
If you are trying to solve the problem of having to do the above on existing data, you can do this manually, just to build up that t_keyword_count table first (depends on how many keywords you have in total, but even if there are a lot, this can be scripted). But you should change (or mirror) the t_msg.msg_text field to be a field suitable for text search, and use SQL text search functionality to find the keywords.

mysql search across joined fields

I have customer table with fileds for first name and last name plus other details.
In a form a user can enter a search which I have working but on unique fiels only eg searches for john in any of first name or last name. This all works ok
However I dont know how to search if a user enters john smith which would be a combination of two fields. How can I do this search with my current data structure?
Ive tried adding a field in the select statement using:
Concat_ws (' ', firstname, lastname) as fullname
But I cant use where on this generated field and throws an error
Should I create a new field to contain full name?
Thank you
You don't use aliases in where condition. instead use the complete function
e.g.
where Concat_ws(' ', firstname, lastname) like '%<your search>%'
But, yes, it is good idea to create a new field to optimize performance
And make sure you dont have space after the function name
It depends on which combinations of fields you want. For your example firstname = 'john' AND lastname='smith' would do the job, but its probably not what you need.
The best solution would be to use a fulltext search.