I've 2 table.
Table1
Has count about 2700 rows
Colums: ID, NO, NAME
Table2:
Has count about 300 rows
Colums: ID, NAME
where:
Table1.NO = Table2.ID
I want to list Table1(2700 rows) but if Table1 doesn't contain some of Table2's rows I want to write "NA"
How can i do that with SQL?
I assume you want to output the Name from table2, if it's present, in which case:
SELECT
a.id,
isnull(b.name, 'NA'),
a.name
FROM
table1 a
LEFT JOIN
table2 b
ON
a.no = b.id
will do it for you (I've also output the id and name from table1).
EDIT:
Apologies, I didn't see the MySQL tag until I posted. You will need to use the coalesce function instead if isnull, like so:
SELECT
a.id,
coalesce(b.name, 'NA'),
a.name
FROM
table1 a
LEFT JOIN
table2 b
ON
a.no = b.id
Try LEFT JOIN and ISNULL function like this below
SELECT ISNULL(Table1.Name,'NA')
FROM Table1
LEFT JOIN Table2
ON Table1.NO = Table2.ID
Related
The result of this query gives me 0 rows where it should give me the 3 latest rows (grouped by Table1.Name).
Table1 has: "Name", "Timestamp", "Voltage".
Table2 has: "Name", "data".
When I delete "ORDER BY Table1.Timestamp" I do get 3 rows (as expected) but they are the 3 oldest entries in the database where I want the 3 latest.
(I have 3 Name values in Table1 and Table2 that match).
The code:
SELECT * from Table1
INNER JOIN Table2
ON Table1.Name=Table2.Name
GROUP BY Table1.Name
ORDER BY Table1.Timestamp;
You can try to perform a query like this :
SELECT t.*,t2.* from Table1 t
INNER JOIN Table2 t2
ON t.Name=t2.Name
WHERE t.Timestamp = (
SELECT MAX(t3.Timestamp) FROM Table1 t3
WHERE t3.Name = t.Name
)
You could sort and taking the top 3:
SELECT * from Table1
INNER JOIN Table2
ON Table1.Name=Table2.Name
ORDER BY Table1.Timestamp DESC
LIMIT 3
When you are using join two or more tables then don't use * in select query. Use specific column name to avoid column ambiguously in select query , you can also use Table1., Table2. but good way to use specific columns in select query.
SELECT Table1.Name,Table1.Timestamp from Table1
INNER JOIN Table2
ON Table1.Name=Table2.Name
GROUP BY Table1.Name
ORDER BY Table1.Timestamp;
I'm trying to select from one table with the count of another table where the id matches the original tableID sort of like:
select *, (count(0) from table2 where table2.table1ID = table1.table1ID) count
from table1
What's the mySQL syntax for this?
SELECT COUNT(b.*) FROM table1 as a
LEFT JOIN table2 as b ON a.tableID = b.tableID
select
table1.*,
if(table2.table1ID is null,0,count(*))
from
table1
left join table2 on table1.table1ID = table2.table1ID
group by
table1.table1ID;
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 I'm pulling information from.
Lets say table1 has following columns
(id, title, category, sub_category, sub_sub_category)
Lets say table2 has following columns
(category_id, category_name)
I have a select statement that so far looks as follows:
SELECT
table1.id,
table1.title,
table2.category_name as Cat1,
table2.category_name as Cat2,
table2.category_name as Cat3
FROM
table1,
table2
INNER JOIN table2 as c1 ON c1.category_id = table1.category
INNER JOIN table2 as c2 ON c2.category_id = table1.sub_category
INNER JOIN table2 as c3 ON c3.category_id = table1.sub_sub_category
WHERE
table1.id = ?
This gives me an error about table1.category being an unknown column
I have also tried
SELECT
table1.id,
table1.title,
table2.category_name as Cat1,
table2.category_name as Cat2,
table2.category_name as Cat3
FROM
table1,
table2
WHERE table1.id = ?
AND table1.category = table2.category_id
AND table1.sub_category = table2.category_id
AND table1.sub_sub_category = table2.category_id
The last example at least gives me column output I'm looking for which would be
(table1.id, table1.title, table1.category name, table1.sub_category name...)
So showing the category name from table 2 instead of the ID's. I am an amateur coder and haven't had to use inner joins before but maybe that is what I need to do. I just can't figure out how to get it to output the data I need.
Thank you in advance for your time and consideration.
Your problem is that you have a comma in the from clause. Simple rule: never use commas in the from clause. Always use explicit join syntax.
Then, you also have table2 mentioned an extra time, and your select is pulling columns from the wrong instance of table2.
The fixed up query looks like:
SELECT t1.id, t1.title,
c1.category_name as Cat1, c2.category_name as Cat2,
c3.category_name as Cat3
FROM table1 t1 INNER JOIN
table2 c1
ON c1.category_id = t1.category INNER JOIN
table2 c2
ON c2.category_id = t1.sub_category INNER JOIN
table2 c3
ON c3.category_id = t1.sub_sub_category
WHERE t1.id = ?;
Ups, you have a couple of problems here.
First table2 can have only one column called category_name so there is no reason to select it three times like you do, result of that would be that you have three absolutely same column with different name.
Second the syntax is completely wrong. Syntax for INNER JOIN is
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
also you need to have column in both table usually foreign key and that column will be use to match data. I guess in your case it should be category_id from second table and in first table that's column you named category (it, should be renamed at category_id it's more convenient)...
So if understand right you want to select id, title from first table and category_name from second you could do that like this:
SELECT table1.id, table1.title, table2.category_name
FROM table1
INNER JOIN table2
ON table1.category_id = table2.category_id;
And friendly advice to you is to find some online sources and learn a little bit more about this before you continue with what ever you do...
Query 1:
SELECT sum(total_revenue_usd)
FROM table1 c
WHERE c.irt1_search_campaign_id IN (
SELECT assign_id
FROM table2 ga
LEFT JOIN table3 d
ON d.campaign_id = ga.assign_id
)
Query 2:
SELECT sum(total_revenue_usd)
FROM table1 c
LEFT JOIN table2 ga
ON c.irt1_search_campaign_id = ga.assign_id
LEFT JOIN table3 d
ON d.campaign_id = ga.assign_id
Query 1 gives me the correct result where as I need it in the second style without using 'in'. However Query 2 doesn't give the same result.
How can I change the first query without using 'in' ?
The reason being is that the small query is part of a much larger query, there are other conditions that won't work with 'in'
You could try something along the lines of
SELECT sum(total_revenue_usd)
FROM table1 c
JOIN
(
SELECT DISTINCT ga.assign_id
FROM table2 ga
JOIN table3 d
ON d.campaign_id = ga.assign_id
) x
ON c.irt1_search_campaign_id = x.assign_id
The queries do very different things:
The first query sums the total_revenue_usd from table1 where irt1_search_campaign_id exists in table2 as assign_id. (The outer join to table3 is absolutely unnecessary, by the way, because it doesn't change wether a table2.assign_id exists or not.) As you look for existence in table2, you can of course replace IN with EXISTS.
The second query gets you combinations of table1, table2 and table3. So, in case there are two records in table2 for an entry in table1 and three records in table3 for each of the two table2 records, you will get six records for the one table1 record. Thus you sum its total_revenue_usd sixfold. This is not what you want. Don't join table1 with the other tables.
EDIT: Here is the query using an exists clause. As mentioned, outer joining table3 doesn't alter the results.
Select sum(total_revenue_usd)
from table1 c
where exists
(
select *
from table2 ga
-- left join table3 d on d.campaign_id = ga.assign_id
where ga.assign_id = c.irt1_search_campaign_id
);