How to do a "NOT EXISTS"?

2,004 views
Skip to first unread message

Scott Royston

unread,
Mar 24, 2013, 1:55:15 PM3/24/13
to scala...@googlegroups.com
I'd like to do a query like:
SELECT A.x, A.y FROM A WHERE NOT EXISTS (SELECT * FROM B WHERE B.x == A.x AND B.y == A.y)

is there any way to do this in slick?

Stefan Zeiger

unread,
Mar 27, 2013, 1:47:25 PM3/27/13
to scala...@googlegroups.com
On 2013-03-24 18:55, Scott Royston wrote:
I'd like to do a query like:
SELECT A.x, A.y FROM A WHERE NOT EXISTS (SELECT * FROM B WHERE B.x == A.x AND B.y == A.y)

for {
  a <- A if !B.filter(b => b.x === a.x && b.y === a.y).exists
} yield (a.x, a.y)


--
Stefan Zeiger
Typesafe - The software stack for applications that scale
Twitter: @StefanZeiger

See you at Scala Days 2013 in NYC!
June 10th - June 12th
www.scaladays.org

Adam Mackler

unread,
Dec 11, 2014, 4:33:24 PM12/11/14
to scala...@googlegroups.com
On Wednesday, March 27, 2013 1:47:25 PM UTC-4, Stefan Zeiger wrote:
SELECT A.x, A.y FROM A WHERE NOT EXISTS (SELECT * FROM B WHERE B.x == A.x AND B.y == A.y)
for {
  a <- A if !B.filter(b => b.x === a.x && b.y === a.y).exists
} yield (a.x, a.y)

Thanks Stefan; this would be a good one to put in the documentation.  Also wondering if there shouldn't be a Query.forall() method to use for this?

--
Adam Mackler
 
 

Naftoli Gugenheim

unread,
Dec 14, 2014, 12:47:08 AM12/14/14
to scala...@googlegroups.com
Hmm, if we want to be collections-like, wouldn't it be better if it was q.exists(f) or q.filter(f).nonEmpty?

--

---
You received this message because you are subscribed to the Google Groups "Slick / ScalaQuery" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaquery+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalaquery/4c8b30a0-b80d-4f78-8192-5873e658457d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Christopher Vogt

unread,
Dec 14, 2014, 7:01:41 AM12/14/14
to scala...@googlegroups.com
I agree. You wanna write a ticket and or open a PR :)?

Chris

On 14.12.14 06:47, Naftoli Gugenheim wrote:
> Hmm, if we want to be collections-like, wouldn't it be better if it was
> q.exists(f) or q.filter(f).nonEmpty?
>
> On Thu, Dec 11, 2014, 4:33 PM Adam Mackler <adamm...@gmail.com
> <mailto:adamm...@gmail.com>> wrote:
>
> On Wednesday, March 27, 2013 1:47:25 PM UTC-4, Stefan Zeiger wrote:
>
>> SELECT A.x, A.y FROM A WHERE NOT EXISTS (SELECT * FROM B WHERE
>> B.x == A.x AND B.y == A.y)
> for {
> a <- A if !B.filter(b => b.x === a.x && b.y === a.y).exists
> } yield (a.x, a.y)
>
>
> Thanks Stefan; this would be a good one to put in the
> documentation. Also wondering if there shouldn't be a
> *Query.forall()* method to use for this?
>
> --
> Adam Mackler
>
>
>
>
> --
>
> ---
> You received this message because you are subscribed to the Google
> Groups "Slick / ScalaQuery" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to scalaquery+...@googlegroups.com
> <mailto:scalaquery+...@googlegroups.com>.
> <https://groups.google.com/d/msgid/scalaquery/4c8b30a0-b80d-4f78-8192-5873e658457d%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.
>
> --
>
> ---
> You received this message because you are subscribed to the Google
> Groups "Slick / ScalaQuery" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to scalaquery+...@googlegroups.com
> <mailto:scalaquery+...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/scalaquery/CANpg8PAqNYBKCa7LHFLGcb%3DVX39TD9mE51cAM4igmFTVhavL%3DQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/scalaquery/CANpg8PAqNYBKCa7LHFLGcb%3DVX39TD9mE51cAM4igmFTVhavL%3DQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.

Wentao Zheng

unread,
May 30, 2015, 6:27:39 PM5/30/15
to scala...@googlegroups.com
I'm trying with your solution, but it doesn't seem to compile with the following error messages

[error]  found   : slick.lifted.Rep[Boolean]

[error]  required: Boolean

[error]         user <- Users if user.id != ui.id && !Relationships.filter(_.fromUserId === ui.id).filter(_.toUserId === user.id).exists

Naftoli Gugenheim

unread,
May 31, 2015, 3:36:07 PM5/31/15
to scala...@googlegroups.com
Don't use !=, use =!=

--

---
You received this message because you are subscribed to the Google Groups "Slick / ScalaQuery" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaquery+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalaquery/27d90b68-f19b-47dd-80ff-2cf7ed8bbe5c%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages