I have two Tables [Users] and [Teams]. My Users Table looks like this [ID][E-Mail]. My Teams Table looks like this [Team][UserID].
I need to do a Subselect like this:
SELECT * FROM users WHERE 'ID' != (SELECT UserID FROM teams)
The Problem is that the Subselect returns multiple values. So i cannot compare it like this.
What i want to achive:
I want the Data from the User who IS NOT listed in the Table [Teams].
Example:
Table [Users] | Table[Teams] |
-------------- ------------------
[ID] [E-Mail] [UserID] [Team]
1 example1#google.com | 1 Football
2 example2#google.com | 1 Basketball
3 example3#google.com | 2 Basketball
So as you can see there are 3 exisiting Users, but the third User is not listed in the Table[Teams]. I want the E-Mail from the User, because he is not in any of these teams.
How could i accomplish that?
Instead of != use NOT IN:
SELECT * FROM users WHERE ID NOT IN (SELECT UserID FROM teams WHERE UserID IS NOT NULL)
Notice the UserID IS NOT NULL in sub query... NOT IN would fail if sub query returns one or more NULL values.
you can have multiple ways to implement the same
the below is one of the way
SELECT *
FROM users
WHERE ID NOT IN (SELECT UserID FROM teams)
We could also use an EXISTS clause:
SELECT *
FROM users u
WHERE NOT EXISTS (SELECT 1 FROM teams t WHERE u.ID = t.UserID);
Related
I have two columns account_number and customer_id. A single customer can have multiple account but a single account can't have multiple customer.
I have dumped a file containing account_num and its corresponding customer_id to db through LOAD DATA INFILE command. Now I am trying to validate through query does any account which has come multiple times in a file has same customer_id or different customer_id in two different rows.
REQUIREMENT : i want to return those accounts which has come multiple times but having diferent customer ids
I tried with group by , but didn't get desired result.
This is my query which is not giving the desired result
SELECT ACCOUNT_NUM,UNIQUE_CUSTOMER_ID,COUNT(UNIQUE_CUSTOMER_ID)
FROM LINKAGE_FILE
GROUP BY ACCOUNT_NUM, UNIQUE_CUSTOMER_ID
HAVING COUNT(ACCOUNT_NUM) > 1 AND COUNT(UNIQUE_CUSTOMER_ID) = 1;
Hope I am clear.
You can simply get the count of unique customer ids using COUNT(DISTINCT..) for every account_num and filter out those cases where count is more than 1, inside the HAVING clause:
SELECT
ACCOUNT_NUM,
COUNT(DISTINCT CUSTOMER_ID) AS unique_customer_count
FROM LINKAGE_FILE
GROUP BY ACCOUNT_NUM
HAVING unique_customer_count > 1
Drop the customer check into a join query like so
DROP TABLE if exists t;
create table t(accountid int,cid int);
insert into t values
(1,1),(1,2).(1,1),(2,3),(3,4),(3,4);
select distinct t.accountid,t.cid
from t
join
(
select accountid,count(distinct cid) cids
from t
group by accountid having cids > 1
) s on s.accountid = t.accountid;
+-----------+------+
| accountid | cid |
+-----------+------+
| 1 | 1 |
| 1 | 2 |
+-----------+------+
2 rows in set (0.00 sec)
You can use EXISTS :
SELECT lf.*
FROM LINKAGE_FILE lf
WHERE EXISTS (SELECT 1 FROM LINKAGE_FILE lf1 WHERE lf1.ACCOUNT_NUM = lf.ACCOUNT_NUM AND lf1.UNIQUE_CUSTOMER_ID <> lf.UNIQUE_CUSTOMER_ID);
However, you can also aggregation with your query :
SELECT ACCOUNT_NUM, COUNT(DISTINCT UNIQUE_CUSTOMER_ID)
FROM LINKAGE_FILE
GROUP BY ACCOUNT_NUM
HAVING COUNT(DISTINCT UNIQUE_CUSTOMER_ID) > 1;
By this, you can get only ACCOUNT_NUMs which have two or more CUSTOMER_IDs.
Simple question, but I'm drawing a blank. Any help is appreciated.
I have a table of ids:
-------
| ids |
-------
| 1 |
| 5 |
| 7 |
-------
Except the actual table is thousands of entries long.
I have a list (x), not a table, of other ids, say 2, 6, 7. I need to see which ids from x are not in the ids table.
I need to get back (2,6).
I tried something like this:
SELECT id FROM ids WHERE id IN (2,6,7) GROUP BY id HAVING COUNT(*) = 0;
However, COUNT(*) returns count of retrieved rows only, it doesn't return 0.
Any suggestions?
Create a temporary table, insert the IDs that you need into it, and run a join, like this:
CREATE TEMPORARY TABLE temp_wanted (id BIGINT);
INSERT INTO temp_wanted(id) VALUES (2),(6),(7);
SELECT id
FROM temp_wanted t
LEFT OUTER JOIN ids i ON i.id=t.id
WHERE i.id IS NULL
Try something with "NOT IN" clause:
select * from
(SELECT 2 as id
UNION ALL
SELECT 6 as id
UNION ALL
SELECT 7 as id) mytable
WHERE ID not in (SELECT id FROM ids)
See fiddle here
I have 5 Database, Let say their name is A B C D E
All database have the same table / structure / field
I want to get result from 5 database using table SMSOutgoing and the field is uid
It look like this :
SELECT * OR JOIN 5 database A B C D E F
FROM `table` SMSOutgoing
WHERE uid = 1
Not all the database have uid=1, it need to display which database have the result
I run SMS Gateway, each phone / 1 number using 1 database, thats why there is so many different database.
I spent hours to solve it but always error, I think i follow the wrong guide (JOIN multiple table in 1 database)
I'm Lost, please Help and Thank You
Sounds like you want to list the databases out that contain uid = 1 in the SMSOutgoing table. If so, you should be able to use UNION:
SELECT DISTINCT 'DatabaseA' WhichDb
FROM DatabaseA.SMSOutgoing
WHERE uid = 1
UNION
SELECT DISTINCT 'DatabaseB' WhichDb
FROM DatabaseB.SMSOutgoing
WHERE uid = 1
UNION
...
UNION
SELECT DISTINCT 'DatabaseF' WhichDb
FROM DatabaseF.SMSOutgoing
WHERE uid = 1
I used DISTINCT in case you could have multiple uid in the same table -- that may be unnecessary.
EDIT: From your comments, it sounds like you just want the results:
SELECT *
FROM DatabaseA.SMSOutgoing
WHERE uid = 1
UNION
SELECT *
FROM DatabaseB.SMSOutgoing
WHERE uid = 1
UNION
...
UNION
SELECT *
FROM DatabaseF.SMSOutgoing
WHERE uid = 1
You may need to use UNION ALL if you might have duplicates...
I have table that has 2 fields userId and ebayitemId. Following is table from database:
userId | ebayitemId
12 | 1
12 | 2
12 | 3
12 | 4
In my situation, the client makes request with ebayitemId to see what other items are listed with this user.( ebayitemId is unique ). So far I am using two query to select all items listed by the user. First query is
SELECT userId WHERE ebayitemId = '1'
This query gets me USERID FOR THAT EBAYITEMID.
The second query is
SELECT ebayitemId WHERE userId = '$userid'
This gives me ebayitemId 1,2,3 and 4.
My question: Is there a way to combine these two queries into one query to get above result since only one table is involved?
The query :
SELECT iu.ebayitemId
FROM t_items AS iu
INNER JOIN t_items AS ii ON (iu.userId=ii.userId)
WHERE (ii.ebayitemId= $item )
and if you don't want the first item to be selected :
SELECT iu.ebayitemId
FROM t_items AS iu
INNER JOIN t_items AS ii ON (iu.userId=ii.userId)
WHERE (ii.ebayitemId= $item )
AND (iu.ebayitemId<>ii.ebayitemId)
Note : an IN statement would be less optimized.
It can be done with
SELECT ebayitemId from table WHERE userId in (SELECT userId from table WHERE ebayitemId = '1')
Naively:
SELECT ebayitemId
FROM yourtable
WHERE userId IN (
SELECT userId
FROM yourtable
WHERE ebayitemId = '1'
)
Note there are other ways to skin this cat with joins etc.
I have 9 fields and I need to see all the data from these fields which have a particular set of IDs. Could any one tell me the SQL query for it?
Ex: Database contains 100 records. I need to select a list of 20 IDs from the field BusID and it's corresponding rows.
SELECT *
FROM `Buses`
WHERE `BusID` I am stuck after this.. how do I put in the list of 20 BusIds here?
If you know the list of ids try this query:
SELECT * FROM `Buses` WHERE BusId IN (`list of busIds`)
or if you pull them from another table list of busIds could be another subquery:
SELECT * FROM `Buses` WHERE BusId IN (SELECT SomeId from OtherTable WHERE something = somethingElse)
If you need to compare to another table you need a join:
SELECT * FROM `Buses` JOIN OtheTable on Buses.BusesId = OtehrTable.BusesId
You can try this
SELECT * FROM Buses WHERE BusID in (1,2,3,4,...)
I strongly recommend using lowercase field|column names, it will make your life easier.
Let's assume you have a table called users with the following definition and records:
id|firstname|lastname|username |password
1 |joe |doe |joe.doe#email.com |1234
2 |jane |doe |jane.doe#email.com |12345
3 |johnny |doe |johnny.doe#email.com|123456
let's say you want to get all records from table users, then you do:
SELECT * FROM users;
Now let's assume you want to select all records from table users, but you're interested only in the fields id, firstname and lastname, thus ignoring username and password:
SELECT id, firstname, lastname FROM users;
Now we get at the point where you want to retrieve records based on condition(s), what you need to do is to add the WHERE clause, let's say we want to select from users only those that have username = joe.doe#email.com and password = 1234, what you do is:
SELECT * FROM users
WHERE ( ( username = 'joe.doe#email.com' ) AND ( password = '1234' ) );
But what if you need only the id of a record with username = joe.doe#email.com and password = 1234? then you do:
SELECT id FROM users
WHERE ( ( username = 'joe.doe#email.com' ) AND ( password = '1234' ) );
Now to get to your question, as others before me answered you can use the IN clause:
SELECT * FROM users
WHERE ( id IN (1,2,..,n) );
or, if you wish to limit to a list of records between id 20 and id 40, then you can easily write:
SELECT * FROM users
WHERE ( ( id >= 20 ) AND ( id <= 40 ) );
I hope this gives a better understanding.
You're looking for the IN() clause:
SELECT * FROM `Buses` WHERE `BusID` IN (1,2,3,5,7,9,11,44,88,etc...);
Try the following code:
SELECT *
FROM users
WHERE firstname IN ('joe','jane');
You want to add the IN() clause to your WHERE
SELECT *
FROM `Buses`
WHERE `BusID` IN (Id1, ID2, ID3,.... put your ids here)
If you have a list of Ids stored in a table you can also do this:
SELECT *
FROM `Buses`
WHERE `BusID` IN (SELECT Id FROM table)
I have 3 fields to fetch from Oracle Database,Which is for Forex and Currency Application.
SELECT BUY.RATE FROM FRBU.CURRENCY WHERE CURRENCY.MARKET =10 AND CURRENCY.CODE IN (‘USD’, ’AUD’, ‘SGD’)