Doctrine where not - php

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

Related

get first x rows using in()

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)

How to retrieve related values from 2 different mysql tables (check the example)

I have two mysql tables like following.
Table1 : subjectcodes
guideno | subcode
--------------------
1 | 11
1 | 22
2 | 33
2 | 44
3 | 11
3 | 55
Table2 : seniority
no | guideno
---------------------
1 | 1
2 | 2
I wish to find the guideno from table seniority with subcode same as any of subcodes of guideno 3's.
That is the result is : guidenno 1 (Because guideno 3 and 1 have same subject code 11)
It's burning my head. Any ideas for a solution?
Thanks in advance.
SELECT x.*
FROM subjectcodes x
JOIN subjectcodes y
ON y.subcode = x.subcode
AND y.guideno <> x.guideno
WHERE y.guideno = 3;

circular/round loop based on ID (start with specific ID add the rest of the ID, break when specific ID reached)

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 |
+------+

selecting a set of rows with specific record data

I here today with a question that has left me rather confused and puzzled. Perhaps someone out there could give me a bit of assistance.
I have three table as followed. Please note that they are simplified for this question.
//table_checks//
check_id |task_id| status
1 | 1 | Done
2 | 1 | Done
3 | 1 | Done
4 | 2 | Done
5 | 2 | Not Done
6 | 2 | Not Done
7 | 2 | Not Done
8 | 2 | Done
9 | 3 | Done
10 | 3 | Done
11 | 3 | Not Done
12 | 3 | Done
//table_user//
user_id | email | type
1 | a#a.com | IN
2 | b#b.com | IN
3 | c#c.com | EX
//table_chk_usr//
check_id |user_id
1 | 1
2 | 1
3 | 1
4 | 2
5 | 2
6 | 2
7 | 2
8 | 2
9 | 3
10 | 3
11 | 3
12 | 3
Here are three tables with its relation in the table_chk_usr table.
My question is how do I query and select rows of table_user with type 'IN' from table table_user where all of users assigned task_ids in the table_checks are with the status = done.
So the expected result should be as follows
//table_user//
user_id | email | type
1 | a#a.com | IN
Since user_id with 1 has completed all task_ids with status done.
I hope this makes sense to those of you who are reading. Any help will be much appreciated.
Please Note that I am using PHP as my server side language, if that helps in any way.
I think this is what you are looking for.
select user.user_id, user.email, user.type,
sum(if(status = 'done',1, 0)) as done, count(*) as checks
from checks
join chk_user on checks.check_id = chk_user.check_id
join user on chk_user.user_id = user.user_id
group by chk_user.user_id
having done = checks
sqlFiddle
You could use a join here, but there may be a better way to do it.
SELECT * FROM table_checks
INNER JOIN table_chk_usr
ON table_checks.check_id = table_chk_usr.check_id
INNER JOIN table_user
ON table_chk_usr.user_id = table_user.user_id
WHERE table_checks.status = 'Done'
AND table_user.type = 'IN'

MySQL query question

I have a simple question regrading MySQL. Is it possible to return the rows between row 'x' and row 'y'? It's sort of hard to explain - for the sake of an example: Return rows 6 through 10, excluding rows 1-5 and rows 11+. Thanks! ;D
Use LIMIT. Remember to combine it with ORDER BY for the results to make any sense.
SELECT fields, ...
FROM table
ORDER BY something_sensible
LIMIT 5, 5
(Start from row 6, take 5 rows)
SELECT * FROM table LIMIT 5, 5
http://dev.mysql.com/doc/refman/5.5/en/select.html and look at LIMIT section
Yes, here's an example:
SELECT * FROM myTable LIMIT 5, 5
From the manual (http://dev.mysql.com/doc/refman/5.0/en/select.html):
The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).
With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):
SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
mysql> select * from employees order by emp_id;
+--------+-----------------+---------+
| emp_id | name | boss_id |
+--------+-----------------+---------+
| 1 | f00 | NULL |
| 2 | ali later | 1 |
| 3 | megan fox | 1 |
| 4 | jessica alba | 3 |
| 5 | eva longoria | 3 |
| 6 | keira knightley | 5 |
| 7 | liv tyler | 6 |
| 8 | sophie marceau | 6 |
+--------+-----------------+---------+
8 rows in set (0.00 sec)
mysql> select * from employees order by emp_id limit 2,4;
+--------+-----------------+---------+
| emp_id | name | boss_id |
+--------+-----------------+---------+
| 3 | megan fox | 1 |
| 4 | jessica alba | 3 |
| 5 | eva longoria | 3 |
| 6 | keira knightley | 5 |
+--------+-----------------+---------+
4 rows in set (0.00 sec)
Why don't you use an Auto Increment field? Or you can use LIMIT keyword like:
SELECT * FROM tablename WHERE LIMIT 0, 5
This will show records 1,2,3,4,5

Categories