PhpMyadmin added additions parameter in Select Query - mysql

I have tried to run a select query in phpmyadmin Sql tab as below
Select * from followers
I just run above query alone
But it return following Error in the image, can anybody help to fix this issue? can you tell me in what case it will happends?

You should use comma for adding others column in order by
Select * from followers
order by follwer_id DESC, leader_id
limit 30
or if you want filter the query you should use where
Select * from followers
where leader_id =1
order by follwer_id
limit 30

Related

In mysql how do I only search through the first 10 records

I am writing a query, but I only want to search the first 10 records in the table. I know that in a select limit usually limits the records, but it doesn't work for this instance.
eg
SELECT * FROM `logon` WHERE `username`='superman' ORDER BY `user_id` LIMIT 10
The above line will never work because the query only returns one.
I only want to search through the first 10 records, and limit doesn't work in this case.
So how do I limit my search to the first 10 records?
SELECT * FROM
(SELECT * FROM `logon` ORDER BY `user_id` LIMIT 10) as temp
WHERE temp.`username`='superman';
You can simply write a subquery which returns the "first" 10 records and put a where clause on the result set.
SELECT *
FROM `logon`
WHERE `username`='superman'
and user_id in (select user_id from logon order by user_id limit 10)
(I haven't tried this, but I think it's the fastest way to do this)
Use order by
SELECT * FROM `logon` WHERE `username` = 'superman' ORDER BY user_id LIMIT 10
Not the right Way...but can be done...
SELECT *
FROM logon
WHERE username='superman' AND srno BETWEEN 1 AND 10;

Is there an equivalent of SQL_CALC_FOUND_ROWS in SQL Server?

I have this mssql query:
with RESULT as(select TITLE, URL, ROW_NUMBER() over (order by URL) as SeqValue from WEBSITE
select * from RESULT where SeqValue>=20 and SeqValue<=40
I'd like to know how many record this query would return if the where statement was not there.
I try with select count(*) from RESULT and try with ##ROWCOUNT and many others way but did not work.
I need TITLE and URL from select, and in the end i need total record for the select.
For example in mysql query i have prepareStatement using SQL_CALC_FOUND_ROWS:
select SQL_CALC_FOUND_ROWS TITLE, URL from WEBSITE limit ?, ?
and after this select i have:
select FOUND_ROWS()
In this example returned value is total record for the mysql query.
Total record is same with LIMIT and without LIMIT directive.
I convert database from mysql to mssql and i have problem with this.
Please help me...
I had the same concern when trying to move from MySQL to SQL SERVER
the solution is to inject this in your query:
COUNT (*) OVER () AS ROW_COUNT
ROWCOUNT appear in all rows in the result then it only remains for you to retrieve ROW_COUNT from the first row result (if exists) and not forget to reset the result pointer et Voila;-).
for example:
select COUNT (*) OVER () AS ROW_COUNT, URL from WEBSITE limit ?, ?
I hope it will help
"RESULT" is going to include every record in WEBSITE, right? So just "select count(*) from WEBSITE".
I would expect
with result as(...what you have...)
select count(*) from result;
To work too, with some unnecessary work. What does that not do that you want?
try:
declare #row_count int
select #row_count = count(*), URL from WEBSITE limit ?, ?
select #row_count
I know it is too late, but can be helpful with pagination and limits.
WITH paging AS (SELECT ROW_NUMBER() OVER(ORDER BY {$sortField} {$sortOrder}) as rowId, count(1) over() as ROW_COUNT, key FROM {$table})
SELECT * FROM {$table} result INNER JOIN paging ON result.key = paging.key WHERE rowId BETWEEN {$start} and {$end}
It has the property for sorting field, sort order and key is used to join the original table. The start and end are the values of your pagination.
Now you have total number of records on ROW_COUNT field with required fields. It also starts from 1 and not from 0.

SQL First() Function

I am using phpMyAdmin to write some SQL code that I thought was simple but proving to be a headache. I'm using this tutorial to help me out. My goal is to get the first and last columns id's from a result set. When I do this query I get 5 rows starting at 15 and going through 11.
SELECT id
FROM boardPost
WHERE recipientId = 1
ORDER BY id DESC
LIMIT 0,5
However, when I try this query I get an error #1064: "You have an error in your SQL syntax."
SELECT FIRST(id)
FROM boardPost
WHERE recipientId = 1
ORDER BY id DESC
LIMIT 0,5
Something like this maybe?
SELECT min(id), max(id)
from (
select id
from boardPost
where recipientId = 1
order by id desc
limit 0,5
) t
I think that is what you want?
Select id from boardPost order by id asc limit 1
and
Select id from boardPost order by id desc limit 1
If you just want the first and the last id of a result set, you could consider this:
SELECT MIN(id) firstId, MAX(id) lastId FROM someTable WHERE aField = 1;
Note that it'll only work if you do use and ORDER BY an AUTO_INCREMENT field, else you might get unexpected results.
It'll only work with the full set. If you need the first and last id of a limited one, you're probably better of using 2 queries with ASC and DESC order and LIMIT 1.
MySQL does not support the FIRST() function. You will need to use the workaround they specified in that tutorial (using ORDER BY and LIMIT)
In some situations (like mine, that first brought me here), there are some rarely-used MySQL "windowing functions" such as FIRST_VALUE and LAST_VALUE that may provide the functionality you're seeking.
(Here's more info on Window Function Concepts and Syntax, and the Wikipedia description of the term.)

Syntax error (operator missing) in query-expression?

I'm trying to understand COUNT(*), and therefore I created a testing query:
SELECT COUNT(*)
WHERE COUNT(UITLENINGEN.LLNR) >= 30;
When I click Execute, I get the following error:
Syntax error (operator missing) in query-expression COUNT(*) WHERE COUNT(UITLENINGEN.LLNR) >= 30.
What am I doing wrong?
Try this
SELECT COUNT(*) FROM UITLENINGEN GROUP BY LLNR HAVING COUNT(UITLENINGEN.LLNR) >= 30;
I don't understand what you're trying to do. The query below is based on a table which includes a field named category_id. And it uses GROUP BY category_id to count the number of rows within each such group. The HAVING clause limits the result set to only those groups whose count is at least 30.
SELECT category_id, COUNT(*)
FROM YourTable
GROUP BY category_id
HAVING COUNT(*) >= 30;
If that is nothing like what you're trying to accomplish, please give us more detailed information so we may better understand your situation. A brief set of sample data, and the output you want based on that sample, would help tremendously.
You have not specified the table from which the data should be retrieved. Try the following
SELECT COUNT(*) from tableName
WHERE COUNT(UITLENINGEN.LLNR) >= 30;
Add your table name to the query.
SELECT COUNT(*) FROM UITLENINGEN WHERE COUNT(UITLENINGEN.LLNR) >= 30;
Please, add table name and use having statement where aggregation funcion required. E.g.:
select count(*)
from UITLENINGEN
having count(UITLENINGEN.LLNR) >= 30;

MySQL Query order by if null

I am trying to build a MySQL query that has an order by statement. This is what I am trying to do:
SELECT *
FROM tbl_product
ORDER BY retail_price ONLY IF wholesale_price IS NULL OTHERWISE ORDER BY wholesale_price.
I have no idea where to start. I found an article that uses ORDER BY COALESCE but I also found that this could have performance issues.
Any advice is appreciated.
SELECT *
FROM tbl_product
ORDER BY ifnull(wholesale_price, retail_price);
Note that you don't need to select the value you're ordering by - it can be any expression