Hello there, I have a schema like this, table name feeds
Where msg_id is unique and its the primary key of the table
| msg_id |commented|
| 1 | 10 |
| 2 | 10 |
| 3 | 10 |
| 4 | 21 |
I want to build a query that would select the last two rows
The output should go like this
| msg_id |commented|
| 3 | 10 |
| 4 | 21 |
In short the query should return the rows with msg_id which have a distinct commented value
Group by the column ment to be unique and select the highest id for every group
select max(msg_id) as msg_id, commented
from your_table
group by commented
Try this -
select max(msg_id), commented from your_table group by commented
SELECT * FROM feeds GROUP BY commented
Related
So my table looks like this:
| id | user | points |
| 1 | Sam | 1 |
| 2 | Sam | 6 |
| 3 | Phil | 1 |
The query I am currently using is:
SELECT user,COUNT(*) FROM table GROUP BY user order by COUNT(*) DESC
This returns the current value:
Sam: 2
Phil: 1
It looks like it counts the number of rows, not the total points? How can I do this?
The correct return should be Sam: 7.
Use SUM instead of COUNT
SELECT user, SUM(points) FROM table GROUP BY user
I have a table as following:(Ex)
id | vid | time
------------------------
1 | 4 | 1333635317
2 | 4 | 1333635323
3 | 2 | 1333635336
4 | 4 | 1333635343
5 | 5 | 1333635349
I want to be just a row (the last row [ID: 4]) of the same rows[id:1,2,4], how it will output the query?
I mean, as a result of these:
id | vid | time
------------------------
3 | 2 | 1333635336
4 | 4 | 1333635343
5 | 5 | 1333635349
What do i do?
i trying it as:
SELECT * from tbale as t1 where vid = 4 GROUP BY vid ORDER BY id DESC
but doesn't work ORDER BY in my query.
Get the max time per vid and use in to get those rows from the table.
select * from tablename
where (vid,time) in (select vid,max(time)
from tablename
group by vid)
order by id
How to query for erase the view below?
+-------------------+------------+
| Order_id | Weight |
| 20 | 4 |
| 21 | 5 |
| 22 | 2 |
| 22 | 2 |
+-------------------+------------+
To be like this:
+-------------------+------------+
| Order_id | Weight |
| 20 | 4 |
| 21 | 5 |
| 22 | 2 |
| 22 | |
+-------------------+------------+
When displaying results but not entered into the database.
A simple way is:
select DISTINCT order_id, weight from xyz
UNION
select order_id, null from xyz
group by order_id, weight
having count(*) > 1
Order by weight desc;
The 1st select statement will display all the unique values and 2nd one will retrieve only the repeated values.
In your required output table, it seems like you want to display all the non-repeated rows and the 1st column value of repeated rows but not 2nd column value. The above query will allow you to do that.
OK, here is how to do it:
SELECT
Order_id,
Weight,
if(#order_id = Order_id, '', Weight) as no_dup_weight,
#order_id := Order_id as dummy
FROM Table1
ORDER BY Order_id asc;
You basically need to check to see if the previous Order_id is the same as the current, and if they are, output an empty field.
Here is an SQLFiddle demonstrating the solution.
Do you actually need 2 rows for the dupes? Can't you just use the DISTINCT clause as per http://www.mysqltutorial.org/mysql-distinct.aspx
Or is it important to know what has duplicates. In which case you should look into the GROUP BY clause
I have a table has columns tag_id and item_id,
---------------------
| tag_id | item_id |
---------------------
| 1 | 2 |
| 2 | 2 |
| 4 | 2 |
| 3 | 5 |
| 5 | 7 |
| 11 | 5 |
---------------------
For example, I want to return item_id = 2 if I input tag_id = 1,2,4
what is the query should look like?
I am using Codeigniter.
If you are looking for Msql query that returns only one cell value you can use the following:
SELECT DISTINCT item_id FROM tableName WHERE tag_id IN (1, 2, 4)
Try this code.Will help you
$this->db->select('item_id');
$this->db->from('tblNAME');
$where = "tag_id='1' OR tag_id='2' OR tag_id='2 ";
$this->db->where($where);
return $this->db->get()->result();
Try this:
select * from table where tag_id in (1,2,4)
Thank you all answers but unfortunately not suitable for the use. Because I want it return exactly the item id = 2 if tag id = 1,2,4.
If we using IN(1,2,4) it will return item ids that has tag id 1 or 2 or 4.
Finally I figure out the SQL query should be
Select *, COUNT(item_id) AS id_count
FROM TABLE_NAME
WHERE tag_id IN(1,2,4)
GROUP BY item_id HAVING id_count = 3
Therefore, express it in a general form, we simply replace the 3 by the size of the array of tag id.
I have a query in my PHP where I'm searching for string:
select * from feed1 where PNAME like '%clothes%' limit 5;
I also want to get count of PNAME like cloth for which I'm using separate search query:
$qry=mysqli_query(select * from feed1 where PNAME like '%clothes%');
$rows=mysqli_num_rows($qry);
Rows query is taking too much time to load the page. Is there any way we can get total row count and limit of up to 5 products from one query? Thereby, the load time will decrease.
+-------+------------+----------+--------------+-------------+-----------+------
-------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardi
nality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+------
-------+----------+--------+------+------------+---------+---------------+
| xml | 0 | PRIMARY | 1 | BOSID | A |
7233 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+------
-------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
You can't do this by one query, but for get count, use COUNT(*) instead of get all the data out.
To get the count:
select count(*) from feed1 where PNAME like '%clothes%'
then get first 5:
select * from feed1 where PNAME like '%clothes%' limit 5
There should be at least two queries for pagination.