Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oL5eb-0001l5-PS for pgsql-sql@arkaria.postgresql.org; Mon, 08 Aug 2022 16:30:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oL5ea-0003JV-Db for pgsql-sql@arkaria.postgresql.org; Mon, 08 Aug 2022 16:30:56 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oL5ea-0003JM-4R for pgsql-sql@lists.postgresql.org; Mon, 08 Aug 2022 16:30:56 +0000 Received: from mout.perfora.net ([74.208.4.197]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oL5eX-0002E0-Pm for pgsql-sql@lists.postgresql.org; Mon, 08 Aug 2022 16:30:55 +0000 Received: from [10.221.133.171] ([142.105.135.230]) by mrelay.perfora.net (mreueus002 [74.208.5.2]) with ESMTPSA (Nemesis) id 0MC3qI-1oCFwp48TQ-008v9N for ; Mon, 08 Aug 2022 18:30:52 +0200 Message-ID: <7f08717a-6784-46aa-a22c-50d295e1fdeb@sopchak.me> Date: Mon, 8 Aug 2022 12:30:50 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Subject: Re: select items based on 2 columns Content-Language: en-US To: pgsql-sql@lists.postgresql.org References: From: Carl Sopchak In-Reply-To: Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Provags-ID: V03:K1:vDQ+xut96dQEn6gTiKTwZey8bRKa2uc3aQ5ikFmt+VafSkrNEPH 2gS+6CH05bVZS1NNF23o97d2IHZ7qL2d4Cp+GC6FYDGD9IBBcU4I0/M64n63ZN305UbLPq5 +tyLuy6DjDqHTHLJQ7ZVF0GXBiM+xzpwWN5iVuxLEsy6inuE0Yv1DpLSVKe6XLTsuIM5yFi 8t9AswwEf8u49jj6udvHQ== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:+PpMm97ZTAw=:uFvKuA7KR154vcmPSs1XvB VLFVGjLklQNQmSVmVDS4wd4S0OaCMhvMQdHyfj9GevTKFsi/CinETSV35TW77qvMWrMA8a46t 17LNR+9xebyPrcs2XdmsMdyaOmgy/2QwyYYxKfAOlCFmAbzoBZaQ9ounkcwO9UUcSxtI7mzuE EeDN2fR7t9Si7Cgo2RzyXhuUQmeF7p6pP2nSWIyVv5stI7+r4axLo+00T3ll0lrm0SYaujCcU jgeMZPqsu6Uoq6b/Cmu1rIGGbLHEXzBsGFHSaAq7H7N8xgF5UhKKWabXOzq4o/kw+J6vkK5cw Ibdrq6VdENlh7k89VFr/TKsu3l5DuetL7+NYtk927JnRdNHTqanH6DdCg+xfn0ZosGUFuxw20 LQL1BDiT0Kptg8ePNfUWCmFbLnZDnlnDQMVWOFe5k/8nK+/VC0aJCCWF/bsSoYNaMsTwSueQj itYBYgxtsV2ZN17SdIUnRZWWHpwB0oR01+K8iW0IW9CTl1vLnM/1nFLX4qrqcBatZu+XbJBnD ve1Xy8Yr2ctkPXxoFOv7I28JdVRQ+ITrrC5LVidI8Yov7vgPZ9BdnL+ylmK09gqwPP3qaWX8B 9eLhPKb6FSidX5OrlkZEqVwlcGrxGyVlfV7aNMHmF/CN9EhCSAiIqDf/YkWmdweE/gx+yj+NL kO21LVXIYSbKzPNZ4GVNNUE6jeBe60+XBR9j/e+cpGamU7nDFG+2sAC8IpsUAdBMRQ+qd5ioD 6v/6cvZw7+8UiV29YcITqgY8tBrKlmLDIcyemg== List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk

You can also quote column names that are SQL keywords, so "in" and "out" would also work, but I also agree that it's not great practice.  However, IMHO, IN and OUT doesn't tell you a lot.  In and out of what?  Inbound/outbound?  In_water / out_of_water?  You get the idea.  Using clear column names helps prevent misuse (intentional or not).

On 8/8/22 12:17, Pierre Chevalier wrote:
Hello,

Short answer:
SELECT id FROM tbl WHERE in and out;


BUT this will never work as is.


Longer answer:
First of all, it would be better not to name fields with word having a meaning in SQL: "in" is not accepted as a valid field name.
>From now on, to get rid of this problem, I renamed the in and out fields by appending an "x" at the end: "in" becomes "inx" and "out" becomes "outx".

Here is the complete answer; I added some data matching your criteria:

-- Create the table:
CREATE TEMPORARY TABLE tbl (id integer, id_b integer, inx boolean, outx boolean);

-- Put some data into it:
INSERT INTO tbl (id, id_b, inx, outx) VALUES
(51, 57, false, false),
(51, 42,  true, false),
(51, 32, false, false),
(51, 76, false, true),
(51, 49,  true, false),
(51, 47,  true, true);

-- Select id where inx is true and outx is true:
SELECT id FROM tbl WHERE inx and outx;

-- Output:
┌────┐
│ id │
├────┤
│ 51 │
└────┘
À+
Pierre

Le 08/08/2022 à 10:24, Shaozhong SHI a écrit :
The following is the type of data:

id  id_b   in    out
51 57     false false
51 42     true   false
51   32   false  false
51   76   false  true
51  49     true   false


How to do the following:

select id from tbl   if in is true and out is true are found?

Regards,

David


-- 
Pierre Chevalier