How to declare a variable in MySQL?

There are mainly three types of variables in MySQL: User-defined variables (prefixed with @):You can access any user-defined variable without declaring it or initializing it. If you refer to a variable that has not been initialized, it has a value of NULL and a type of string.SELECT @var_any_var_name You can initialize a variable using SET or SELECT statement:SET @start = 1, @finish = … Read more

Case statement in MySQL

Yes, something like this: As other answers have pointed out, MySQL also has the IF() function to do this using less verbose syntax. I generally try to avoid this because it is a MySQL-specific extension to SQL that isn’t generally supported elsewhere. CASE is standard SQL and is much more portable across different database engines, and I prefer to … Read more

What’s the difference between VARCHAR and CHAR?

VARCHAR is variable-length. CHAR is fixed length. If your content is a fixed size, you’ll get better performance with CHAR. See the MySQL page on CHAR and VARCHAR Types for a detailed explanation (be sure to also read the comments).

MySQL query String contains

Quite simple actually: The % is a wildcard for any characters set (none, one or many). Do note that this can get slow on very large datasets so if your database grows you’ll need to use fulltext indices.

SQL SELECT WHERE field contains words

Rather slow, but working method to include any of words: If you need all words to be present, use this: If you want something faster, you need to look into full text search, and this is very specific for each database type.

Using group by on multiple columns

Group By X means put all those with the same value for X in the one group. Group By X, Y means put all those with the same values for both X and Y in the one group. To illustrate using an example, let’s say we have the following table, to do with who is attending what subject at … Read more