I am having two tables, the structure is given below
Table 1
schid
name
cost
type
Table 2
schid
details
oldcost
I am unable to write a query to display records from table 2 of let suppose type A OR B (Here as you can see type field is in table 1), Here one more thing to add is that schid is not a primary key, The query which i am executing is retrieving more records than expected, I think due to join, Can i execute it without using join
SELECT *
FROM Table1
JOIN Table2 ON Table1.schid=Table2.schid
WHERE Table1.type='A'
OR Table1.type='B'
This would help:
SELECT t2.schid, t2.details, t2.oldcost
FROM Table2 t2
JOIN Table1 t1
ON t1.schid = t2.schid
WHERE t1.type IN ('A', 'B');
This should retrieve only the table 2 records which match the criteria.
SELECT t2.*
FROM Table2 t2
JOIN Table1 t1 ON t1.schid = t2.schid
WHERE t1.type = 'A'
OR t1.type = 'B';
SELECT t2.*
FROM `Table2` t2
JOIN `Table1` t1 ON t2.`schid`=t1.`schid`
WHERE t1.`type` IN ('A','B');
Related
I'm trying to perform a SQL query like this:
SELECT
t1.*
FROM
`table1` t1,
`table2` t2
WHERE
t1.x = 1
and the table called table2 (t2) is empty but inside t1 there are entries.
For example this query works perfect:
SELECT
t1.*
FROM
`table1` t1
WHERE
t1.x = 1
So just by adding the second table t2 in the FROM part the query gives no results anymore. So I don't understand what is going on here. In my case it should be possible that one of the tables is empty but the query still needs to give results. How can I achieve this?
Your syntax is using an implied JOIN between table1 and table2, and could be rewritten as:
SELECT t1.*
FROM `table1` t1,
CROSS JOIN `table2` t2
WHERE t1.x = 1
This is JOINing everything in table1 against everything in table2. However, as table2 is empty there is nothing to join to.
With an implied CROSS JOIN, results are only returned when the row on both sides of the join is matched, which in this case it cannot be. Therefore, nothing is returned.
If you rewrote the statement to use a LEFT JOIN, you would see all results from table1, and only matching results from table2:
SELECT t1.*
FROM `table1` t1,
LEFT JOIN `table2` t2 ON 1 = 1
WHERE t1.x = 1
Incidentally, typically you would only use this kind of query if there is a relationship in the data between table1 and table2. In this case, you would JOIN on the related columns, like so:
SELECT t1.*
FROM `table1` t1,
LEFT JOIN `table2` t2 ON t2.matchedColumn = t1.matchedColumn
WHERE t1.x = 1
I need some help with a specific problem about subqueries in MySQL. This is a shortversion of the problem I have:
I have table 1 and 2 where table 1 have a column called "SupplyID" which corresponds with table 2's "ID". I need to get a list with stuff from one single supplier.
I tried with this but didn't work:
select name
from item
where SupplyID exists (
select *
from SupplyID
where SupplyID = ID
);
Assuming your tables are named table1 and table2 you
You could use a inner join
select distinct t1.name
from ybale as t1
inner join table2 as t2 on t1.ID = t2.SupplyID
Try this:
select name from item i where exists (select * from table2 t where i.SupplyID = t.ID);
My answer is more like to what scaisEdge answered here but I strongly recommend to do this with LEFT JOIN. and If you want to select just one ID, for example ID=10
SELECT T1.name
FROM item AS T1
LEFT JOIN SupplyID AS T2 ON T2.SupplyID=T1.ID
WHERE T1.ID = 10
I have 2 tables:
table1 (id,usedcode)
table2 (codeid,uniquecode)
I want to be able to check if a certain value exists in uniquecode of Table2, but is not already used in Table1
Try using left join as below:
SELECT t2.*
FROM table2 t2 LEFT JOIN table1 t1
ON t2.uniquecode = t1.usedcode
WHERE t1.usedcode IS null
SELECT uniquecode FROM Table2
WHERE NOT EXISTS(
SELECT * FROM Table1 WHERE usedcode = uniquecode
)
In English the query is saying, "Select all unique codes from table 2 that don't exist in table 1 as a usedcode".
The following will get me values from a reference table t2 I want to insert to or to update existing tuple with in table t1:
SELECT
id, col1
FROM
t2
LEFT OUTER JOIN
t1
ON
t2.id=t1.id
If a tuple with id already exist in t1, it should be updated with the value selected from t2. If a tuple with id does not exist in t1, (id, col1) should be inserted with other columns set to default values.
How to do this efficiently?
use this two querys:
This will join and filter, giving you the values that exists in both tables, so you just do the update
Update t1 set t1.col1 = t2.col1
from t1 inner join t2 on t1.id = t2.id
This will join and filter, giving you the values that are in t2, but not in t1, so you just do the insert.
insert into t1 select t2.id, t2.col1
from t2 left outer join t1 on t2.id = t1.id where t1.id IS NULL
UPDATE:
as I can see from here MySQL uses another sintax for this. So you query may work with this instead of the query above:
UPDATE t1 temp1
INNER JOIN t2 temp2
ON temp1.id= temp2.id
SET temp1.col1= temp2.col1
but the concepts are the same (just a different syntax)
You won't need the Where, because the INNER JOIN will only use fields that match/join.
I have 2 tables for which I need to run a query on
Table1 has 2 fields: l_id, and name
Table2 also has 2 fields: l_id, and b_id
I need to run a query to get the "name" and "l_id" for all the entries in table1 that do not have an entry in table2 for a given b_id.
Hope this makes some sense
select t1.*
from Table1 t1
left outer join Table2 t2 on t1.l_id = t2.l_id
and t2.b_id = #SomeValue
where t2.l_id is null
You can use an outer join, but I find a sub-query is a little more straightforward. In your case selecting everything from table1 that does not have an id in table2. Reads better...
SELECT * FROM table1 WHERE l_id NOT IN (SELECT l_id FROM table2);