difference betweene in and = MYSQL [duplicate] - mysql

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
MYSQL OR vs IN performance
I was wondering whats the difference between:
SELECT * FROM table WHERE cat = 'cat1' OR cat = 'cat2' OR cat = 'cat3'
And:
SELECT * FROM table WHERE cat in ('cat1', 'cat2', 'cat3')
Is there any difference? As I tried they both give out same result.

They are identical the IN() is just a short-hand version using listing out all of the OR statements.

IN() just makes for a much shorter, and easier to read, syntax especially when you have a lot of OR clauses.

There is one more thing that you can do quite easily with IN but you can't with =.
SELECT *
FROM `table`
WHERE `column` IN (
SELECT col_name
FROM `table2` -- or the same table
WHERE `some_column` = 5
)
So basically you search in a subset of another table, which sometimes comes in handy.
Real life usage:
You have a list of administrable user types (each with its own permissions) in another table, and you want to enforce that the user type actually exists.

Related

Why does `select *, column from table;` work but `select column, * from table;` not work [duplicate]

This question already has answers here:
Why does adding '*' to a MySQL query cause a syntax error?
(3 answers)
Closed 5 years ago.
Sometimes its handy to return all data in a table as well as the data you specifically want e.g.
SELECT *, name, age FROM users;
But it would be more handy to get the data you want first, followed by the remaining data.
SELECT name, age, * FROM users;
However the second SQL statement fails with a syntax error.
Why is this?
I've searched a bit and I found a total of 3 questions on SO that are related to this question or pose the same question. Disclaimer: I only found them by searching for something from the link Damien provided in the comments: "Use of an unqualified * with other items"
You get these SO questions:
Why does adding '*' to a MySQL query cause a syntax error?
Using count(*) and * in the same select clause gives an error. Why?
Confusing SQL error in SELECT NULL, *, NULL, NULL
In the comments on the answer of the last question I found a link to a bug report from 2007 which was closed in 2009 with the following explanation:
I have updated http://dev.mysql.com/doc/refman/5.1/en/select.html to
reflect the behavior:
A select list consisting only of a single unqualified * can be used as
shorthand to select all columns from all tables:
SELECT * FROM t1 INNER JOIN t2 ...
tbl_name.* can be used as a qualified shorthand to select all columns
from the named table:
SELECT t1., t2. FROM t1 INNER JOIN t2 ...
Use of an unqualified * with other items in the select list may
produce a parse error. To avoid this problem, use a qualified
tbl_name.* reference
SELECT AVG(score), t1.* FROM t1 ...
Highlight/Bold provided by me. I couldn't find any further explanation for this behaviour or what kind of parse error could occur or where it would occur.
If someone has some time this could be answered by digging into MySQL's or MariaDB's (which has the same behaviour) source code but short of that I don't see someone finding a real answer to this.
Because mysql is lenient to its syntax, but not always...
Beside, if you want to write a valid SQL query, it should be like that:
select col1, col2, users.*
from users
Or with alias on table and column:
select u.*, 'foobar' as col1, 1 as col2
from users u

what is the default order for a simple SELECT in a MySQL table with AUTO_INCREMENT column [duplicate]

This question already has answers here:
Default sort-ordering in MySQL (ALTER TABLE ... ORDER BY ...;)
(2 answers)
MySQL default order depends on WHERE [duplicate]
(4 answers)
Closed 9 years ago.
I have a table (mytable) with an AUTO_INCREMENT column (id_mt). When I do a simple query like
SELECT * FROM mytable;
The result is ordered by the AUTO_INCREMENT column like with
SELECT * FROM mytable order by id_mt;
So my question is : what are the rules (if there are) for the result order when you don't use 'order by' AND if you have an AUTO_INCREMENT column ?
To be clear for some who refer to other posts : I don't use
ALTER TABLE mytable ORDER BY sort_order ASC;
It is clear that this query just change the performances when you use an ORDER BY query.
My question was more 'Does the AUTO_INCREMENT change (force ?) the order of a simple SELECT ?'
From the DBA Stack Exchange site:
In the SQL world, order is not an inherent property of a set of data. Thus, you get no guarantees from your RDBMS that your data will come back in a certain order -- or even in a consistent order -- unless you query your data with an ORDER BY clause.
So, to answer your question, MySQL sorts the records however it wants without any guarantee of consistency.
If you are just curious about the internals of MySQL, Rolando provides an interesting answer.
If, on the other hand, you intend to rely on this order for anything, you must specify your desired order using ORDER BY. To do anything else is to set yourself up for unwelcome surprises.

Query to get records present in one table but NOT in the other [duplicate]

This question already has answers here:
Mysql: Select rows from a table that are not in another
(10 answers)
Closed 9 years ago.
I have two tables:
teacher_lm and teacher.
These tables have both the column "teacher_email"
What I need is to get the emails that are present in teacher_lm but not in teacher.
I have 2 different approaches to solve this, but I don't understand why one of them doesnt give any result and the other one returns a lot of rows.
First one: 842 rows
SELECT DISTINCT lm.teacher_email
FROM teacher_lm as lm
WHERE NOT EXISTS (SELECT * FROM teacher as lt
WHERE lt.teacher_email = lm.teacher_email
)
Second one: no results
SELECT DISTINCT lm.teacher_email FROM
teacher_lm AS lm
WHERE lm.teacher_email NOT IN
(SELECT lt.teacher_email FROM
teacher AS lt)
Could you tell me what I'm doing wrong here, and what's the best way to do this?
Thank you.
The "in" subquery probably has a NULL in it.
Try this instead:
SELECT DISTINCT lm.teacher_email
FROM teacher_lm AS lm
WHERE lm.teacher_email NOT IN (SELECT coalesce(lt.teacher_email, '')
FROM teacher AS lt)
By the way, I think the first version is the version recommended for mysql for optimization reasons.
Your first query is working on the existence of an entire row.
The second query may be returning nulls. I don't believe the NOT IN clause works well in the presence of nulls.
Gordon's answer corrects this by replacing nulls with an empty string.

Is mysql count(*) much less efficient than count(specific_field)? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
COUNT() vs. COUNT(1) vs. COUNT(pk): which is better?
count() and count(column_name), what's the diff?
count(*) vs count(column-name) - which is more correct?
The benefit of using count(*) in a select statement is that I can use it with any table and that makes automating scripts easier:
count_sql = 'select count(*) ' + getRestOfSQL('tablename');
But, is it less efficient than using count(specific_field)?
For InnoDB
If specific_field is not nullable, they are equivalent and have the same performance.
If specific_field is nullable, they don't do the same thing. COUNT(specific_field) counts the rows which have a not null value of specific_field. This requires looking at the value of specific_field for each row. COUNT(*) simply counts the number of rows and in this case can be faster as it does not require examining the value of specific_field.
For MyISAM
There is a special optimization for the following so that it does not even need to fetch all rows:
SELECT COUNT(*) FROM yourtable
Generally, it wouldn't matter so much, as we're returning the same number of rows.
This link covers it nicely
count(*) vs count(column-name) - which is more correct?
Count(*) vs Count(1)
This link also explains more, specifically with Oracle

SELECT * or SELECT specific columns [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Which is faster/best? SELECT * or SELECT column1, colum2, column3, etc
Is there a big performance impact in doing a SELECT * FROM table_name than doing SELECT specific_columns FROM table_name?
Yes, it affect performances, especially when you select multiple rows.
Select only fields you really need.
Let's take a simple username existance check as example:
Why you'd select everything, when you can select only ID? Both doing the job, but selecting only one field is much better solution.
SELECT `id` FROM users WHERE `username` = 'Nikola K.'
rather than:
SELECT * FROM users WHERE `username` = 'Nikola K.'
I would suggest using EXPLAIN to find the best method to suit your needs. I suppose it would depend on how many columns you have in your table.
The following are some useful sites on MySQL Explain:
http://dev.mysql.com/doc/refman/5.0/en/explain.html
http://dev.mysql.com/doc/refman/5.0/en/using-explain.html
http://weevilgenius.net/2010/09/mysql-explain-reference/
Select only what you need! When in development I use * because it's easier when I don't really know what I'll be needing, but in the final version you really should be specific. Why not?