I Have a problem with getting data from the database.
Actually, I want multiple data from the database with passing multiple same ids using IN in MySQL.
Like
table
SELECT * FROM table WHERE id IN (1,2,3,1,2)
I need result with five records.
OUTPUT like,
You can use LIMIT to limit the results to only 5 records and use ORDER BY FIELD to get the desired order. Here's a snippet:
SELECT * FROM table WHERE id IN (1,2,3) ORDER BY FIELD(id, 1, 2, 3) LIMIT 5;
Related
I'm doing an info system that has announcements, so I wanted to get the data from announce table, first should be the one with max id of course since it is the latest announcement and here is my code for it:
SELECT image FROM announce where id=(SELECT max(id) FROM announce);
Now I wanted to get the data before the max id and I'm using this code here:
SELECT image FROM announce where id=(SELECT max(id)-1 FROM announce);
But it only works if there are no deleted rows,if there are some deleted rows it doesn't work like max id is 10 and the id's 9,8,7 are deleted. That means the present id's are: 1,2,3,4,5,6,10, how will I be able to get the data in the id 6,5,4,3,2,1?
If you ORDER the query by descending order (DESC), and have an OFFSET of one (OFFSET 1), you would get the second to last row. And offset of two would give the row before that, and so on. This also uses LIMIT 1, so we just get one row. LIMIT 1, 1 is the same as LIMIT 1 OFFSET 1.
SELECT image
FROM announce
ORDER BY id DESC
LIMIT 1, 1
MySQL on SELECT statements
You can just use one query with order by and limit instead of the two queries you use:
select image
from announce
order by id desc
limit 2
In case you want to query them separately, you can change limit 2 to limit 1, 1 for you second query to work as you wish.
Let's say I have a table with following columns: id-1, id-2, col-1, col-2, col-3
Here, id-1 is the primary key and is auto-incremented. id-2 is a different id and is not necessary to be unique. There are multiple instances of same id in that column. col-1, col-2, col-3 are just necessary columns.
I pass a query to select data from the table.
mysqli_query($connect, SELECT * FROM table WHERE id-2='some_specific_id')
It will return multiple rows. I would like to know how can I target specific rows, say row number 3.
First, use ":
mysqli_query($connect, "SELECT * FROM table WHERE id-2 = 'some_specific_id'");
Target specific row? Do you mean to limit the fetched rows? Or get the 3rd row?
For limiting the fetched rows, you can use LIMIT:
SELECT * FROM table WHERE id-2='some_specific_id' LIMIT 3
For getting the third row:
SELECT * FROM table WHERE id-2='some_specific_id' LIMIT 2, 1
Well although it seems you just rather needed to learn basic SQL to get your answer, there is still the question in the title, that may attract other people whose problem is formulated exactly like that. So goes the answer:
Mysqli is not very convenient for this task, so we would use PDO.
In case your query is intended to return multiple rows and you need to address one of them by number (which is rather makes little sense, but anyway), use fetchAll() method:
$stmt = $connect->prepare("SELECT * FROM table WHERE id2=?");
$stmt->execute(['some specific id']);
$data = $stmt->fetchAll();
and you will be able to address returned rows by number, starting from zero:
echo $data[0]['col1'];
However, it makes more sense to address the returned rows by some unique id. In this case just add this unique field fiset in the field list and then use the special PDO street magic:
$stmt = $connect->prepare("SELECT id1, table.* FROM table WHERE id2=?);
$stmt->execute(['some specific id']);
$data = $stmt->fetchAll(PDO::FETCH_UNIQUE);
and you will be able to address returned rows by that unique field :
echo $data[$id1]['col1'];
Use LIMIT to get what you want like:
SELECT * FROM table WHERE id-2='some_specific_id' LIMIT 2, 1;
Or, if you want to fetch from array, then use the 3rd index of array.
LIMIT Explanation:
The following illustrates the LIMIT clause syntax with two arguments:
SELECT
column1,column2,...
FROM
table
LIMIT offset , count;
Let's examine the LIMIT clause parameters:
The offset specifies the offset of the first row to return. The offset of the first row is 0, not 1.
The count specifies the maximum number of rows to return.
What would be the fastest way to select 2 random rows from an SQL table ? (SQL only or not)
I am using MySQL on PhpMyAdmin.
Seems like it would be:
SELECT * FROM table LIMIT 2
What you will get is two rows in database default order.
Try this:
SELECT * FROM table ORDER BY RAND() LIMIT 0, 2
i have a query that i use to count the number of rows as a result, but i would also like to print out the most recent row of that query, in a view, but only that recent query instead of the whole array
basically i use this query, then run a count to display the amount of rows which is a count i display, but i also want to display the latest date, location, and code, it can only get it to display all
2 options...
Select * from table order by primary_key desc;
count the results and print the first (most recent) row
or run two separate queries...
select count(primary_key) as num_rows from table;
select * from table order by primary_key desc limit 1;
How to fetch the first two rows from Mysql DB using Mysql PHP function? Is there any function which can give me first 2 or 3 rows from the select query we fired?
Use LIMIT. From the manual, to retrieve 3 rows:
SELECT * FROM tbl LIMIT 3;
Or to retrieve rows 6-15:
SELECT * FROM tbl LIMIT 5,10;
For this query (i.e. with no constraint) if you are not using an ORDER BY clause your results will be ordered as they appear in the database.
You can use the limit clause in your query:
select * from your_table limit 3
This will select the first three rows.
And:
select * from your_table limit 5, 3
The later will select rows starting from 5 and return three rows.
You have 2 options:
Fire a query to select all the rows
and then select 2 or 3 rows as
needed using PHP.
Fire a query to select only the
necessary number of rows using LIMIT
clause.
The 2nd option is preferable.