Here the SQL query and I need some adjustment to it
SELECT DISTINCT ic.img_path, ic.id
FROM images_community ic
WHERE ic.delete_flag = 0 AND ic.status = 1
ORDER BY ( SELECT (count(id = ic.id ) + ic.views)
FROM image_comments WHERE img_id = ic.id) DESC
I need to show record after specific ID
not limit and offset for sure I need display rows after this id =5
this images retrieve
> images id | img_path
> 1 | dafad.sjdbh
> 2 | dafad.sjdbh
> 5 | dafad.sjdbh
> 3 | dafad.sjdbh
IS there a way to display records after id = 5 in the same query retrieved
You can use limit and offset to achieve it, please see Limit, Offset
If you want all rows after offset just do
SELECT DISTINCT ic.img_path, ic.id
FROM images_community ic
WHERE ic.delete_flag = 0 AND ic.status = 1
ORDER BY ( SELECT (count(id = ic.id ) + ic.views)
FROM image_comments WHERE img_id = ic.id) DESC LIMIT (SELECT cout(*) FROM table_name) OFFSET my_row_offset
Found the row position for this ID and start the Limit from it.
Related
I have a table which stores user items, the two key columns which I would like to use in this query are user_id and item_id. The id field in the example is not needed but just added to show these aren't the only two columns in the table.
----------------------
id user_id item_id
----------------------
1 1 324
2 1 324
3 3 324
4 2 230
5 4 324
The query which I would like to construct should return the top 10 users who have the most items with a specific item id.
So for example if I wanted to run the query against the item ID 324 I should get the following result.
-------------------
user_id item_count
-------------------
1 2
3 1
4 1
2 0
try this
select user_id , count(*) as item_count from table
where item_id = 324 group by user_id order by item_count desc limit 10
limit 10 will show you the top 10 users and order by desc sort from high to low.
However, the above query will not give you the 0 count as per your question. If you really want the zero count you can try this: (assuming your table name is userlist)
SELECT distinct user_id,
(select
count(*) from `userlist`
where user_id=u.user_id and item_id=324
) as item_count FROM `userlist` u
order by item_count desc
I couldn't create the database in my local, but I think this will do the trick
SELECT user_id, COUNT(item_id) as item_count
FROM TABLE_NAME
WHERE item_id = 324
GROUP BY item_id
ORDER BY item_count;
I have a table that is something like this
id | names | value
1 Vicky 43
2 Erica 23
3 Rueben 33
4 Bob 54
5 Chris 60
Then I set them in order according to their value. Now the table looks like this.
id | names | value
5 Chris 60
4 Bob 54
1 Vicky 43
3 Rueben 33
2 Erica 23
Now the starting point is id 5 which has a name of Chris and a value of 60. My goal is, to get the next row which has an id of 4 and name of Bob and a value of 54.
You just need to limit the resultset:
SELECT * from table
ORDER BY value DESC
LIMIT 1, 1
Output:
| ID | NAMES | VALUE |
|----|-------|-------|
| 4 | Bob | 54 |
Fiddle here.
The LIMIT basically works this way: the first number sets the starting point (being 0 the minimal value) and the second number the amount of items to fetch (in this case only one).
Edit:
A different way of understanding the question would be: Given a value for a particular field (EG: id field with value of 5)... what would be the previous record? As we have the id 4 in the data we should return that one.
That could be accomplished this way:
SELECT * from t
WHERE id < 5
ORDER BY id DESC
LIMIT 1
Fiddle here.
This way you can traverse the results in both orders (ASC and DESC) and also get both the next or previous (> or <) rows.
If your current ID is for example 4 then
Next:
select * from foo where id = (select min(id) from foo where id > 4)
previous:
select * from foo where id = (select max(id) from foo where id < 4)
sql server:
with temp as
(
SELECT ROW_NUMBER() OVER (ORDER BY value desc) AS ROWID, * FROM table_name
)
SELECT * from temp where ROWID=2
mysql:
SELECT * from table
ORDER BY value DESC
LIMIT 1, 1
I get the feeling that this is a PHP related question?
If that's so, then you can use PHP's mysql or mysqli_fetch functions to get what you want... along with a loop
This is your basic loop-through-a-mysql-query
$sql = mysql_query( "SELECT * from table ORDER BY value DESC" );
while ( $r = mysql_fetch_array( $sql ) ) {
echo $r['value'] . "<br />\n";
}
If you want to have them all at your disposal and be able to call either one of them at will, you will need to store the data in an accessible array, like so
$sql = mysql_query( "SELECT * from table ORDER BY value DESC" );
$theData = array();
while ( $r = mysql_fetch_array( $sql ) ) {
$theData[] = $r['value'];
}
Then, to access the SECOND value, use this
echo $theData[1];
/id/desription/status/
--------------------
/1/testing01/ 1 /
/2/testing02/ 0 /
/3/testing03/ 1 /
/4/testing04/ 1 /
Hi,
I need to query next id where status = 1
If mySQL Query, I need query id next to id = 1 where status = 1
SELECT * WHERE id = (1 + 1) AND status = 1 ?
So my outcome is id number 3.
Thanks for the help,
appreciate lots.
How about order and limit?
SELECT * FROM table_name WHERE status = 1 AND id > 1 ORDER BY id ASC LIMIT 1;
You should look into setting the indexes correctly here also, to ensure top performance.
I have query:
SELECT ID
FROM VACANCIES
WHERE CATEGORYID = 1
AND VISIBLE = '1'
AND user_enable = '1'
AND DATA >= '2012-08-10 10:54:46'
AND torder >= 0
AND ID > 570153
ORDER BY torder ASC, DATA ASC, ID ASC
LIMIT 1
I get the result ID - 570164 as previous key and this is wrong result: correct result is 567556
570164 | ROW 1 | 2012-08-10 11:27:39
567556 | ROW 2 | 2012-08-10 10:55:53
570153 | ROW 2 | 2012-08-10 10:54:46
Is there a solution to get prev id ?
Notice:
I make order by date but date can be equal and prev id can be less than curent ID
Your condition states that the ID must be greater than 570153.
570164 is the result returned, which satisfies that condition.
567556 is the result you're hoping for, which doesn't satisfy that condition.
My guess is you want a less than rather than greater than on your ID condition, but I don't know what you're aiming for so can't be sure.
I'd also switch your sort condition from ASC to DESC on the ID column, or you'll get the first match (i.e. lowest id) rather than the one closest to the value you're comparing to.
SELECT ID
FROM VACANCIES
WHERE CATEGORYID = 1
AND VISIBLE = '1'
AND user_enable = '1'
AND DATA >= '2012-08-10 10:54:46'
AND torder >= 0
AND ID < 570153
ORDER BY torder ASC, DATA ASC, ID DESC
LIMIT 1
I'm not sure exactly how this is called but I'll try to describe as good as I can what I want to acheive.
So, first of all, there is a variable, called $id, which is actually $_GET['id']. Assuming the user is entering the following page by requesting: /page.php?id=6. Now what I need to do is to provide the information about the next 3 pages from database. Here is the database:
TABLE `pages`
id | page_name
______________________
1 | AAAAA
2 | BBBBB
3 | CCCCC
4 | DDDDD
5 | EEEEE
6 | FFFFF
7 | GGGGG
8 | HHHHH
9 | IIIII
So, while requesting the page with id 6, the following script returns the next 3 pages (7,8,9):
$res = mysql_query("SELECT * FROM `pages` WHERE `id`>'".intval($id)."' ORDER BY `id` DESC LIMIT 3");
while($arr = mysql_fetch_assoc($res))
{
print("Page ID: ".$arr['id']."; Page Name: ".$arr['page_name']."\n");
}
And here is the output:
Page ID: 7; Page Name: GGGGG
Page ID: 8; Page Name: HHHHH
Page ID: 9; Page Name: IIIII
And it works fine until the $id is greater then 6. When it is (/page.php?id={7/8/9}), the output doesn't show 3 pages any more, but 2 pages, 1 page and respectively no output when $id is 9.
So my question is: Is there a way to go back and start from the beginning when there are not enough results (less than 3) to display?
When accessing /page.php?id=8, the output should contain pages with id 9, 1 and 2.
When accessing /page.php?id=9, the output should contain pages with id 1, 2, 3.
When accessing /page.php?id=3, the output should contain pages with id 4, 5, 6 and so on.
(SELECT *, 0 AS custom_order FROM `pages` WHERE `id`>'".intval($id)."' ORDER BY `id` ASC LIMIT 3)
UNION ALL
(SELECT *, 1 AS custom_order FROM `pages` ORDER BY `id` ASC LIMIT 3)
ORDER BY custom_order, id ASC
LIMIT 3
This way you always get 3 pages. If not enough next pages, you will get up to 3 from the beginning.
You could modify the query to be something like:
select * from
(select *, id-$inval($id) as order_by
from pages were id > $inval($id) order by id asc limit 3
union
select *, id as order_by
from pages order by id asc limit 3 ) as pages
order by order_by asc
I would solve this way (one possible issue is that the resultset could contain at most 6 records instead of 3):
$res = mysql_query("(SELECT * FROM `pages` WHERE `id`>'".intval($id)."' ORDER BY `id` ASC LIMIT 3) UNION DISTINCT (SELECT * FROM `pages` WHERE id>0 ORDER BY id ASC LIMIT 3)");
$counter = 0;
while($arr = mysql_fetch_assoc($res) && $counter<3)
{
$counter++;
print("Page ID: ".$arr['id']."; Page Name: ".$arr['page_name']."\n");
}