It's not a duplicate question because I saw and didn't find my answer in those questions.
Table example :
id type title
1 3 sth1
2 4 sthelse
3 3 sth2
4 3 sth3
5 4 sth4
6 4 sth5
I want to get 2 first rows of each type. like this:
id type title
1 3 sth1
3 3 sth2
2 4 sthelse
5 4 sth4
I played with group_by nd limit but look at this forexample:
SELECT * FROM `example` WHERE type IN(3, 4) LIMIT 2;
It gives me :
id type title
1 3 sth1
2 4 sthelse
and by using group_by(type) it gives only 1 row from each type.
Note: I don't want to make a loop for that in php that it will be about 15 or 20 queries but if it can be done using 1 query that's nice.
Here ist a sample how you can do it.
1) sort all results by type
2) then count the row of each type
3) get only ROWs where recno < 3
SELECT
#nr:=IF(#lastid = e.TYPE , #nr:=#nr+1 ,1) AS recno,
#lastid:=TYPE AS last_type, e.*
FROM `example`e,
(SELECT #lastid:=0, #nr:=0) tmp
WHERE TYPE IN(3, 4)
HAVING recno < 3
ORDER BY TYPE;
Result:
MariaDB [tmp]> SELECT
-> #nr:=IF(#lastid = e.TYPE , #nr:=#nr+1 ,1) AS recno,
-> #lastid:=TYPE AS last_type, e.*
-> FROM `example`e,
-> (SELECT #lastid:=0, #nr:=0) tmp
-> WHERE TYPE IN(3, 4)
-> HAVING recno < 3
-> ORDER BY TYPE;
+-------+-----------+----+------+---------+
| recno | last_type | id | type | title |
+-------+-----------+----+------+---------+
| 1 | 3 | 1 | 3 | sth1 |
| 3 | 3 | 3 | 3 | sth2 |
| 1 | 4 | 2 | 4 | sthelse |
| 3 | 4 | 5 | 4 | sth4 |
+-------+-----------+----+------+---------+
4 rows in set (0.00 sec)
Related
I use in a database. but I want to use in CodeIgniter. How can I do this with CodeIgniter using the active record query method? Can this be done with COUNT po_id?
SELECT position.po_name AS position, activity.ac_name AS activity ,
COUNT(register.po_id) AS regis FROM register
INNER JOIN activity ON register.ac_id = activity.ac_id
INNER JOIN position ON register.po_id = position.po_id
WHERE register.regis_status = 2 AND activity.ac_id = 2
GROUP BY register.ac_id,register.po_id
regis_status | ac_id | po_id
-----------------------------
2 | 4 | 2
2 | 2 | 2
2 | 2 | 2
3 | 2 | 2
3 | 2 | 2
1 | 3 | 2
2 | 3 | 1
1 | 7 | 2
If the query is correct .Try this
$this->db->select('position.po_name AS position, activity.ac_name AS activity , COUNT(register.po_id) AS regis')
->from('register')
->join('activity', 'register.ac_id = activity.ac_id','INNER')
->join('position ', 'register.po_id = position.po_id','INNER')
->where('register.regis_status', 2)
->where('activity.ac_id', $ac_id)
//more where ->where()
->group_by('register.ac_id,register.po_id');
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
Consider I have array of IDs:
1
2
3
4
5
6
7
8
Let's say I can fetch an array starting with an ID of specific number, let's say 5.
mysql_query("SELECT * FROM farmlist WHERE id > 4 ORDER BY id")
it should give me array of form:
5
6
7
8
Question: Is there a way after reaching the last ID, it would add to array the IDs from the smallest until the ID of where we start, so that we have:
5
6
7
8
1 <~ Add
2 <~ Add
3 <~ Add
4 <~ Add
Try this query:
mysql_query("SELECT * FROM farmlist WHERE id > 4 UNION ALL SELECT * FROM farmlist WHERE id <= 4");
select id from farmlist
order by
(select if( id > 4, id - 4, id + (select max(id) + 1 from farmlist)))
pseudo-code:
res1 = mysql_query("SELECT * FROM farmlist WHERE id > 4 ORDER BY id");
res2 = mysql_query("SELECT * FROM farmlist WHERE id <= 4 ORDER BY id");
array_merge(res1, res2);
and the final res would be in res1
Consider this:
$ids = array(1,2,3,4,5,6,7,8);
$arr = array(5,6,7,8);
sort($ids);
sort($arr);
$min = $arr[0];
foreach($ids as $id){
if($id>=$min) break;
$arr[]=$i;
}
//array will now be (5,6,7,8,1,2,3,4)
That what you wanted?
With mysql this could be done as
mysql> create table test (id int);
Query OK, 0 rows affected (0.10 sec)
mysql> insert into test values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10);
Query OK, 10 rows affected (0.03 sec)
Records: 10 Duplicates: 0 Warnings: 0
mysql> select * from test order by case when id >=5 then 0 else 1 end , id ;
+------+
| id |
+------+
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
| 1 |
| 2 |
| 3 |
| 4 |
+------+
The above query could be even expanded in a range, lets say I want data from 5 till 7 on the top and rest all after that and this could be done as
mysql> select * from test order by case when id >=5 and id<8 then 0 else 1 end , id ;
+------+
| id |
+------+
| 5 |
| 6 |
| 7 |
| 1 |
| 2 |
| 3 |
| 4 |
| 8 |
| 9 |
| 10 |
+------+
I have following table structure for tTable
id | version | parentId
1 | 1 | 1
2 | 1 | 2
3 | 2 | 2
4 | 1 | 3
5 | 2 | 3
6 | 3 | 3
If I execute
SELECT MAX(id) AS maxId, FROM tTable GROUP BY parentId
then it will return below result.
id | version | parentId
1 | 1 | 1
3 | 2 | 2
6 | 3 | 3
But here I would like to a little change in returned result for example I would like to have following result set with max() in tTable.
id | version | parentId
1 | 1 | 1
3 | 2 | 2
*5 | 2 | 3*
id = 5th record. I would like to get other two records with max(id) but want to get different id (I want to provide condition) for parentId = 3.
Is it possible in max() with any condition. I would like to preserve other max id but want only change in specific record?
Edit:
Here 5 (or may be 4) will be coming from dynamic variable. So in case of parentId = 3 there could be any value for id (may be 4 or 5 or 6).
You can use UNION ALL to get the result you want but that may not be a exact solution. thought of putting it once if it helps. See a demo here http://sqlfiddle.com/#!2/78cde/9
(SELECT MAX(id) AS maxId, version, parentId
FROM tTable
WHERE parentId != 3
GROUP BY parentId)
union all
(SELECT id as maxId, version, parentId
FROM tTable
WHERE parentId = 3
order by maxId desc
limit 1,1)
I have a table in DB like that
i want to select all except the record contain (x=1, y=1) i mean the id=8
id | x | y
---------------
1 | 2 | 1
2 | 0 | 1
3 | 5 | 6
4 | 6 | 4
5 | 7 | 4
6 | 7 | 4
7 | 5 | 7
8 | 1 | 1
Try this: see the DEMO
select * from TableName where 1 NOT IN(x,y)
Try this query
SELECT * FROM TableName where x!=1 OR y!=1
Following doctrine should work for you.
Doctrine_Query::Create()
->from("tablename")
->where("x != 1 AND Y != 1")
->fetchArray();
you can try this-
select * from TableName where x!=1 and y!=1
try this
SELECT * FROM test where 1 NOT IN(x) OR 1 NOT IN (y)
see demo... http://www.sqlfiddle.com/#!2/dd008/12