I have 2 tables.
Table_1 has product 3 rows : ID, Quantity and Price.
Table_2 has 2 rows : ID, Special_note.
Not all products have a special note. When a product doesn't have a special note, there is no row for that product in table 2.
I'm trying to use a select query that will get all the information from table_1 but also grab the special note from table_2 when there's one.
The problem I'm having right now is that if there is no special note, it won't grab the information in table_1 at all.
I understand why it's doing it but I don't know how to fix the query so that it returns all the products whether there is a special note or not.
SELECT TABLE_1.ID, QUANTITY, PRICE, SPECIAL_NOTE
FROM TABLE_1, TABLE_2
WHERE TABLE_1.ID = TABLE_2.ID
I simplified the query a little bit for the purpose of this example.
Thanks for your help!
Use a LEFT OUTER JOIN:
SELECT t1.ID, t1.QUANTITY, t1.PRICE, t2.SPECIAL_NOTE
FROM TABLE_1 t1
LEFT OUTER JOIN TABLE_2 t2 ON t1.ID = t2.ID
Update:
To add a WHERE clause, e.g., where quantity >= 1, do this:
SELECT t1.ID, t1.QUANTITY, t1.PRICE, t2.SPECIAL_NOTE
FROM TABLE_1 t1
LEFT OUTER JOIN TABLE_2 t2 ON t1.ID = t2.ID
WHERE t1.QUANTITY >= 1
Related
There is a table where strings are stored by type.
How to write 1 query so that it counts and outputs the number of results of each type.
Tried to do this:
SELECT COUNT(t1.id), COUNT(t2.id)
FROM table t1
LEFT JOIN table t2
ON t2.type='n1'
WHERE t1.type='b2';
But nothing works.
There can be many types, how can this be done?
I think this is what you want:
SELECT t1.type, COUNT(DISTINCT t1.id), COUNT(DISTINCT t2.id)
FROM table1 AS t1
LEFT JOIN table2 AS t2 ON t1.type = t2.type
GROUP BY t1.type
Note that this won't show any types that are only in table2. For that you'd need a FULL OUTER JOIN, which MySQL doesn't have. See How can I do a FULL OUTER JOIN in MySQL? for how to emulate it.
I have 2 tables t1 and t2. Each have a customer ID column. What I am looking for is to join the 2 columns and SUBTRACT the duplicates.
My EG:
Table1 and Table2 with the IDs for each
I have tried a union query. The result I am left with is ID = 1,2,3,4,5,6,7,8,9,10. Where, what I'm after is subtracting 1-5 from Table2 and the result = 6,7,8,9,10.
I hope that makes sense and that someone is able to help. Sorry if this is a bit too simple compared to what you're all used to.
In SQL Server you can use the EXCEPT operator:
select ID
from Table2
except
select ID
from Table1
Mysql does not support it though. Using a an in clause or a left join would work in both servers:
--Using In clause
SELECT ID
FROM Table2
WHERE ID NOT IN
(
SELECT ID
FROM Table1
);
--Using join
SELECT Table2.ID
FROM Table2
left join Table1
on Table2.ID = Table1.ID
where Table1.ID is null
Use left outer join
select * from t1 left outer join t2 on t1.customerid = t2.customerid
I have two tables from which I need to get data in the same SELECT output. The thing is that I need to limit the amount of results.
Say I have an ID column that is unique in table1, but in table2 it has many rows with that ID.
Now I just want to list how many different IDs I have in table1 and some other information stored in table2.
How can I get the desired output I show in the end?
To make my idea clear I used a "messenger" database for an example.
Tables
T1
Id_thread Date
1 13Dic
2 12Dic
T2
Id_thread Message Name
1 Hi Someone
1 Hi to you Someone
2 Help me? Someother
2 Yes! Someother
Desired output
T1.Id_thread T2.Name T1.Date
1 Someone 13Dic
2 Someother 12Dic
I'd join and use distinct:
SELECT DISTINCT t1.id_thread, t2.name, t1.date
FROM t1
JOIN t2 ON t1.id_thred = t2.id_thread
Use a JOIN and GROUP BY:
SELECT t1.Id_thread, t2.Name, t1.Date
FROM t1
JOIN t2 ON t1.Id_thread = t2.Id_thread
GROUP BY t1.Id_thread
Note that if Name is the same for all rows in t2 that have the same Id_thread, that column probably should be in t1. If you fix that, you don't need the JOIN.
Try this:
SELECT DISTINCT T1.Id_thread, T2.Name, T1.Date
FROM T1
LEFT OUTER JOIN T2 ON T1.Id_thread = T2.Id_thread
select T1.Id_thread,T2.Name,T1.Date from T1
inner join T2 on T1.Id_thread = T2.Id_thread
group by T1.Id_thread
order by T1.Id_thread
You haven't specified how you want the limit the results from Table 2. Considering you just want one row, you can use a CROSS APPLY:
Select T1.Id_thread,T2Table.Name,T1.Date From T1
Cross Apply (Select Top 1 T2.Name From T2 Where T2.Id_thread=T1.Id_thread) T2Table
You can specify other conditions in the inner Select statement if you wish.
As I am running this query in MySql:
select count distinct(prim_key) IN table_1 inner join
table_2 on ID group by column name
I want to know total entries of a particular state for all the psu.
the table table_1 tells the total entries by the field agency.
Please help me.
select count(distinct prim_key)
from table_1 t1
inner join table_2 t2 on t1.ID = t2.ID
group by state
Try this::
select
count (distinct(prim_key))
from table_1
inner join table_2 on table1.ID=table_2.ID
group by column name
Suppose I have 2 tables:
Table_1 has |Product_ID|Max_Date
Table_2 has |Invoice_ID|Product_ID|Sale_Date|Amount_Sold
Table_2 stores all the Sales for the products.
I want to join the tables in such a way that I get a SUM(Amount_Sold) from Table_2 grouped by Product_ID but only considering the sales that occured before the Max_Date for each product from Table_1.
I tried
SELECT * FROM
(SELECT * FROM Table_1)a
LEFT JOIN
(SELECT Product_ID,Sale_Date,SUM(Amount_Sold) FROM Table_2 GROUP BY Product_ID)b
ON a.Product_ID=b.Product_ID AND b.Sale_Date<a.Max_Date
but it didn't return the correct sums.
I think the answer might be along these lines, but I have not been able to figure it out...
mysql: How to INNER JOIN a table but limit join to 1 result with the highest vote or count?
Any ideas?
Thanks in advance.
You can include the date criteria in your join conditional, which also makes the joins simpler to read, IMHO.
SELECT t2.product_id, sum(amount_sold)
FROM Table_1 t1 INNER JOIN Table_2 t2
ON t1.product_id = t2.product_id AND t2.sale_date <= t1.max_date
GROUP BY t2.product_id