Select join statement is not working - mysql

I'm trying to make an output array from next statement:
I have 2 tables. In each table there is "material_code" which plays the main role. I want to select material_code from table1 where it'is equal to material_code from table2 and join them where status (from table2) is equal to 0.
This is what I've got so far.
SELECT material_code FROM table1
LEFT JOIN table2 ON material_code WHERE status IS 0
Solution:
SELECT material_code FROM table1
LEFT JOIN table2 ON table1.material_code = table2.material_code WHERE status = 0
Thank everyone.

You should try this one:
SELECT
material_code
FROM table1
LEFT JOIN table2
ON table1.material_code = table2.material_code
WHERE status = 0

Sounds like you want an INNER JOIN
SELECT material_code FROM table1
INNER JOIN table2 USING (material_code) WHERE status = 0
If you use LEFT JOIN, you will get records that only exist in table1 and not in table2. You also want to use USING not ON if the column name is the same in both tables (at least in MySQL).

WHERE status IS 0
should be
WHERE status = 0
Only when for null it have to be IS NULL.
And you also missed the join condition (ON table1.material_code = table2.material_code)

If you have a field 'status' in bot table, you can use an explicit table name before the field name to add a condition only on 'status' of table1 like :
WHERE table2.status = 0

Try this SQL code. INNER JOIN is better in your case.
SELECT tb1.material_code
FROM table1 as tb1
INNER JOIN table2 as tb2 ON tb1.material_code=tb2.material_code
WHERE tb2.status = 0

Related

SQL JOIN with 2 tables because data not complet

i need help with one Statement. I have 2 tables. In one table are my data, but not completed in month 1-12. In other table I have month 1-12. I dont know how to get all my data with all month. and if there are no data for this month he should set 0.
Table1:
Table2:
I tried to use this statment but it dont work...
SELECT table2.produkt, table2.monat
(CASE WHEN table2.wert IS NULL THEN table1.wert ELSE 0 END)
FROM table2 JOIN
table1
ON table1.produkt = table2.produkt
I think you just need left join and coalesce():
select t2.produkt, t2.monat, coalesce(t1.wert, 0) as wert
from table2 t2 left join
table1 t1
on t2.produkt = t1.produkt and t2.monat = t1.monat
You should use left join
SELECT table2.produkt, table2.monat
(CASE WHEN table2.wert IS NULL THEN auftrag.wert ELSE 0 END)
FROM table2 Left JOIN
table1
ON table1.produkt = table2.produkt
Use a left join as opposed to a join. That will return all the items in the left most table (table2) and only those that are linked from the other table. That is assuming that your base table is table2
SELECT table2.produkt, table2.monat
(CASE WHEN table2.wert IS NULL THEN auftrag.wert ELSE 0 END)
FROM table2 LEFT JOIN
table1
ON table1.produkt = table2.produkt
For your Query you can use a Left Join instead of join
SELECT table2.produkt, table2.monat
(CASE WHEN table2.wert IS NULL THEN auftrag.wert ELSE 0 END)
FROM table2
LEFT JOIN table1
ON table1.produkt = table2.produkt
With LEFT JOIN you'll return all the rows of the table you set as FROM, so you can have all the rows from the TABLE2 returning also the TABLE1 rows with a match on your LEFT JOIN Criteria (table1.produkt = table2.produkt), without your case that values would be null
Hope it can be solved your problem!

MySQL select count of another table's rows where the IDs match

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;

How to combine 2 columns from 2 tables and subtract duplicates

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

3 tables and 2 left joins

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
);

How to join only one column?

SELECT * FROM table1
LEFT JOIN table2
ON table1.id = table2.table1_id
WHERE table1.id = 1
I need to join only one column from table 2, say first_name.
How can I do that?
Assuming that you mean "select one column from table 2":
SELECT table1.*, table2.first_name
FROM table1
LEFT JOIN table2
...
The accepted answer is the correct answer but I have encountered a strange error when the tables are in two different databases:
Assuming that table1 is in database1 and table2 is in database2.
Initially I have tried this:
SELECT *, database2.table2.first_name
FROM table1
LEFT JOIN database2.table2
ON database1.table1.id = database2.table2.table1_id
WHERE table1.id = 1
The strange thing is that if I try this query from PHP PDO there were no errors but the result contained all columns from database2.table2 (expected only first_name column).
But if I have tried the same query from phpmyadmin got a sintax error:
Table 'database2.table1' doesn't exist
So, for solve that, then all databases need to be specified implicitly like this:
SELECT database1.table1.*, database2.table2.first_name
FROM database1.table1
LEFT JOIN database2.table2
ON database1.table1.id = database2.table2.table1_id
WHERE database1.table1.id = 1
Take your original code and substitute * with table1.*, table2.YourChosenColumn
SELECT table1.*, table2.YourChosenColumn
FROM table1 LEFT JOIN table2
ON table1.id = table2.table1_id
WHERE table1.id = 1
Do you mean in addition to your already stated query:
SELECT * FROM table1
LEFT JOIN table2
ON table1.id = table2.table1_id
WHERE table1.id = 1 and table1.first_name = table2.first_name