I have a problem creating the SQL query which will select only these distinct records from table A if offer_id column in table A doesnt exists in table B column. How should I do this?
Table A construction:
--------------------------
id | offer_id | user_id
--------------------------
Table B construction
-------------------------------------
id | offer_id | user_id | date
-------------------------------------
So basically I want only select this users records from table A if they were not added to the table B which represents the prove of some action.
Right now I'm selecting distinct records from table A like below but how to make a condition if user_id exists in table B so the data would not be selected?
SELECT DISTINCT offer_id FROM aukcje_licytacja_historia WHERE user_id = :user_id ORDER BY offer_id DESC
You have pretty much described the solution:
select a.*
from a
where not exists (select 1 from b where b.offer_id = a.offer_id);
However, I suspect that you also want user_id to match:
select a.*
from a
where not exists (select 1
from b
where b.offer_id = a.offer_id and
b.user_id = a.user_id
);
Related
I am trying to work out how to perform an SQL UPDATE table column from Table A into Table B. The problem I have is trying to concat multiple values from Table A Column X into Table B Column Y
TableA Structure
id | Interest
1 | Bowling
2 | Swimming
1 | Basketball
TableB Structure
id | Interest_new
1 | null
2 | null
I want Table B to have following data
TableB
id | Interest_new
1 | Bowling,Basketball
2 | Swimming
This is my attempt with SQL query but it doesn't concat , just updated table B with first match
UPDATE TableB
INNER JOIN TableA ON TableB.id= TableA.id
SET TableB.id=CONCAT(TableA.id, ',')
where TableA.id= TableB.id;
You probably intend to use GROUP_CONCAT here, as you want an aggregated CSV output:
UPDATE TableB b
INNER JOIN
(
SELECT id, GROUP_CONCAT(Interest ORDER BY id) Interests
FROM TableA
GROUP BY id
) a
ON a.id = b.id
SET
Interest_new = a.Interests;
However, I actually advocate not even doing this, as storing CSV in your SQL tables is a generally bad idea. Consider just making a view of this data instead:
CREATE VIEW newInterests AS
SELECT id, GROUP_CONCAT(Interest ORDER BY id) Interests
FROM TableA
GROUP BY id;
How can I check if all the items in one table exists within another. For example, consider the following two tables.
With their respective query:
SELECT orderID
FROM orders
WHERE customer_id = 1;
TABLE A
-------
orderID
2
1
6
4
SELECT orderID
FROM orders
WHERE customer_id = 2;
TABLE B
-------
orderID
5
1
7
9
I would not want to output anything because all the items from TABLE A do not exist within TABLE B.
However, I were to have:
SELECT orderID
FROM orders
WHERE customer_id = 3;
TABLE C
-------
orderID
5
I would want to output it because all the items within TABLE C are in TABLE B.
If I were to do a select with TABLE C, I would expect table C to be the output.
I have tried the following query:
SELECT *
FROM (SELECT orderID
FROM orders
WHERE customer_id = 1) A
WHERE A.orderID IN (
SELECT orderID
FROM orders
WHERE customer_id = 2);
If you can to check in a by-customer basis, you can do this:
IF (NOT EXISTS
SELECT NULL
FROM orders
WHERE customer_id = 1 -- "Table A"
AND orderID NOT IN
-- "Table B"
(SELECT orderID
FROM orders
WHERE customer_id = 2)
)
SELECT orderID
FROM orders
WHERE customer_id = 1;
Here the query basically gets the orders from table A which are not in table B. If there is none, it then performs the select at the end. Otherwise, it does nothing.
If you use customer_id = 3 instead, you get one row with orderID 5 ("table C").
One approach would be to left join a table with TABLE B and look for nulls in the resulting sub-query. Here is some (untested) pseudo code:
SELECT *
FROM (
SELECT
table_B.orderID
FROM table_A
LEFT JOIN table_B ON table_A.orderID=table_B.orderID
) x WHERE x.orderID IS NULL
Any rows from table_A that don't have a match in table_B will have a NULL value as table_B.orderID. The outer select you may have to change depending on what type of output you are looking for, but hopefully the principal helps.
I wasn't sure how to word the title, but here is what I am trying to do. I have a table where the id can have multiple entries
id | number
___________
1 | 90
1 | 88
2 | 88
3 | 88
I want a query that will return all ids that don't contain the number 90, so only 2 and 3 in this example. I have tried the below, but it still returns the id of 1 since it also has a number of 88.
SELECT DISTINCT id FROM table WHERE number NOT IN (90)
One way of getting the result is by using NOT EXISTS. Basically what it does it it gets all ID which has 90 in the inner query and the NOT EXISTS only shows all ID not in the inner query.
SELECT A.*
FROM TableName a
WHERE NOT EXISTS (SELECT NULL
FROM TableName B
WHERE a.ID = b.ID
AND b.number = 90)
Here's a Demo.
An alternative is by using LEFT JOIN which yields the same result as above.
SELECT a.*
FROM TableName a
LEFT JOIN TableName b
ON a.ID = b.ID
AND b.number = 90
WHERE b.id IS NULL
Here's a Demo.
You can use subquery:
SELECT id
FROM table
WHERE id NOT IN (SELECT id FROM table WHERE number = 90)
You can use aggregation as illustrated below for better performance:
SELECT ID
FROM YourTable
GROUP BY ID
HAVING NOT INSTR(GROUP_CONCAT(`number`),'90');
Demo on SQL Fiddle.
Probably is something simple, but, let's say I have these:
Table User (id_user, name)
Table A (id_a, name, type, #id_user)
And then I have another one that have only its own id and the other foreign keys
Table B (id_b, #id_user1, #id_user2, #id_a, #id_Something)
So, I need a query that returns ONLY the rows of table A and table B with what they have in common. I've tried INNER JOIN but it returns all rows of Table A where the id_user from there is equal to the id_user from table B. Like, if I have these:
Table User:
id_user name
1 Hey
Table A:
id_a name type id_user
1 a car 1
2 b cat 1
Table B:
id_b id_user id_user2 id_a id_Something
1 1 Doesn't matter 1 Doesn't matter
I need to return only the common row between Table A and Table B (that'll be something like:
id_a name type id_user id_b id_user2
1 a car 1 1
I've tried INNER JOIN but it returns to me everything when the id_user from A = id_user from B. I used this syntax:
SELECT *
FROM B
INNER JOIN A ON A.id_user = B.id_user;
Hope I've made myself clear, thank you a lot.
Is what you're going after: "Show me all the rows in A and B which share the same id_user"
SELECT User.id_user, User.name, a.id_a, b.id_b
FROM User
INNER JOIN A ON a.id_user = User.id_user
INNER JOIN B on b.id_user = User.id_user
lets say I have 2 tables:
Table 1: (customers)
------------------------------------------
id | name | etc... | etc..
Table 2: (blockList)
------------------------
id
I want to know if each customer exists or not in blockList table as I'm looping thru customers table (in a single query, as a seperate field)
like this: SELECT * FROM customers, blockList ORDER BY id DESC
You need to use join, example :
SELECT c.*, b.id AS id_blocklist
FROM customers AS c
LEFT JOIN blocklist AS b ON b.id = c.id
ORDER BY c.id DESC
If you want only records in blocklist, use INNER JOIN
you must specify tables connection
SELECT * FROM customers as c, blockList as b WHERE c.id = b.id ORDER BY id DESC