Why is this MySQL query correct? - mysql

I have a table which has only 3 columns. When I type the following query
select * from MyTable order by 5 and 2;
I get every thing in the table( The result is equal to that of select * from MyTable; ). What I originally expected is that I would get some kind of error. But I didn't get it, why?

order by 5 and 2 is interpreted as order by (5 and 2) which is a constant expression, hence no real ordering is done and data is simply shown in the order it was inserted.

What is happenning here is that 5 and 2 is seen as an expression which is evaluated to 1. However, it shouldn't give a result sorted by first column.
Actually, I think you only get sorted data because you inserted it in sorted sequence. Take a look at this SQLFiddle:
http://sqlfiddle.com/#!2/3e04e/1
The data is not sorted by any of the columns, it is being sorted by a value 1.

5 and 2 are column indexes and they mean 5th column and 2nd column.

select * from MyTable order by 5,2;
It means start with index 5 and bring 2record i.e 5,6,7

Related

Limiting the count query in MySQL?

I am trying to do a simple test where I'm pulling from a table the information of a specific part number as such:
SELECT *
FROM table_name
WHERE part_no IN ('abc123')
This returns 25 rows. Now I want to count the number that meet the "accepted" condition in a specific column but the result is limited to only the 10 most recent. My approach is to write it as follows:
Select Count(*)
FROM table_name
WHERE part_no IN ('abc123') AND lot IN ('accepted')
ORDER BY date DESC
LIMIT 10
I'm having a hard time to get the ORDER BY and LIMIT operations to work. I could use help just getting it to limit appropriately, and I can figure out the rest from there.
Edit: I understand that the operations are happening on the COUNT which only returns one row with a value; but I put the second clip to show where I am stuck in my thought process.
Your query SELECT Count(*) FROM ... will always return exactly one row.
It's not 100% clear what exactly you want to do, but if you want to know how many of the last 10 have been accepted, you could use a subquery - something like:
SELECT COUNT(*) FROM (
SELECT lot
FROM table_name
WHERE part_no IN ('abc123')
ORDER BY date DESC
LIMIT 10
)
WHERE lot IN ('accepted')
The inner query will return the 10 most recent rows for part abc123, then the outer query will count the accepted ones.
There are also other solution (for example, you could have the inner query output a field that is 0 when the part is not accepted and 1 when the part is accepted, then take the sum). Depending on which exact dialect/database you are using, you may also have more elegant options.
Select count returns ONE ROW therefore the ORDER BY and the LIMIT will not work on the results

Why MySQL COUNT without table name gives 1

could you please explain why mysql count function without providing any table name gives 1 as value?
SELECT COUNT(*);
Result: 1
Because in mysql select constant_value command is valid (such as select 2 will return 2) and will return 1 row. Count() function without group by will collapse the resultset and count the number of items in the resultset. In this case 1 row would be returned and count(*) counts that.
Normally all selects are of the form SELECT [columns, scalar computations on columns, grouped computations on columns, or scalar computations] FROM [table or joins of tables, etc]
Because this allows plain scalar computations we can do something like SELECT 1 + 1 FROM SomeTable and it will return a recordset with the value 2 for every row in the table SomeTable.
Now, if we didn't care about any table, but just wanted to do our scalar computed we might want to do something like SELECT 1 + 1. This isn't allowed by the standard, but it is useful and most databases allow it (Oracle doesn't unless it's changed recently, at least it used to not).
Hence such bare SELECTs are treated as if they had a from clause which specified a table with one row and no column (impossible of course, but it does the trick). Hence SELECT 1 + 1 becomes SELECT 1 + 1 FROM ImaginaryTableWithOneRow which returns a single row with a single column with the value 2.
Mostly we don't think about this, we just get used to the fact that bare SELECTs give results and don't even think about the fact that there must be some one-row thing selected to return one row.
In doing SELECT COUNT() you did the equivalent of SELECT COUNT() FROM ImaginaryTableWithOneRow which of course returns 1.
Reference

Select all column but distinct on one column and random

I have the following table:
ID Type Name
1 1 first value
2 1 2nd value
3 2 3rd value
I want to select a row with random Name for each Type. If I use a select with group by Type, it returns two rows, always (first value, 3rd value), but I want to make it random: sometimes first value and sometimes 2nd value. Is there any way to achieve this?
Well, your result is "random" in regards to that you cannot rely on always getting the same result. MySQL just gives you some "random" value for name. However, in order to get some value they just go through the table and pick what they see first or last, so as long as the table doesn't change, the result won't change either.
Knowing so, simply change the rows' order first:
select type, name
from
(
select type, name
from mytable
order by rand()
) randomly
group by type;
SQL fiddle: http://www.sqlfiddle.com/#!2/31560/7.

mySQL - LIMIT syntax to get all the rows after a certain point?

I hope this is a very easy answer:
I can use
LIMIT 2,4
to give me records 3,4,5,6
How can I get all the records from 3 onwards? If I use
LIMIT 2
It will just give me the first 2 rows? Right?
To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 3rd row to the last:
SELECT * FROM tbl LIMIT 2,18446744073709551615;
Taken from the MySQL manual
SQLFiddle demo

Finding the sum of values in a column for the last n rows

I'm trying to find the sum of values in a particular column for the last ten rows selected by some criteria and ordered by some column. I tried the obvious:
SELECT SUM(column) AS abc FROM table WHERE criteria ORDER BY column DESC LIMIT 10
However this seems to sum the entire column!?
So after playing around this seems to work:
SELECT SUM(column) AS abc FROM (SELECT column FROM table WHERE criteria ORDER BY column DESC LIMIT 10) AS abc
My questions...
Why doesn't the more intuitive approach work?
I could access the result by using $data[0], but I prefer to have some meaningful variable. So why do I need to do AS abc twice?
Is there a tidier/better way to do the job?
I'm quite inexperienced with SQL queries so I would really appreciate any help.
Because mysql runs query in the following order:
FROM->WHERE->GROUP BY->HAVING->ORDER BY->LIMIT.
So limit will be applied after grouping and will filter groups but not ordinary rows.
Regarding abs twice: it's necessary to add alias for all derived queries. This is mysql rule.