I have an existing query (Query1) that returns 5 fields from a table. What I want to do is then have another query (Query2) that uses each record from one of the fields returned in Query1 to return fields from a different table. Both tables have a common field (Project). Query1 returns Project and 4 additional fields. For each record returned by Query1 I want to run Query2 based on the Project field. How do I design Query2?
Thanks in advance for your help!
Since both tables share a common field (Project), you should be able to JOIN them in a single query without having to worry about subqueries. Something like this:
SELECT *
FROM Table2
INNER JOIN Table1 ON Table2.Project = Table1.Project;
However, if Query1 is performing a specific operation to only return those 5 records, then you can just JOIN the results of Query1 to that other table. So your Query2 might look like:
SELECT *
FROM Table2
INNER JOIN Query1 ON Table2.Project = Query1.Project;
Related
I have two queries in MS Access. One has a field labeled "Assembly" and the other query has it's field labeled "Assemblies".
I need to compare the two queries (both with only the one column) and find out from the query with the "Assemblies" column which entries MATCH the other query.
Thanks
To find the matching records, a simple inner join will suffice here, e.g.:
select * from query1 inner join query2 on query1.assembly = query2.assemblies
Rename query1 & query2 as appropriate.
Re all,
I need a little help with SQL. I got two SQL queries:
the first one counts how many occurences of a certain condition:
SELECT COUNT(*)
FROM table1
WHERE condition1 = #condition
the second one select each row that match the same condition on the same table joined with a secondary table:
SELECT *
FROM table1 INNER JOIN table2
ON table2.id = table1.id_sub
WHERE condition1 = #condition
Using both these queries in interactive fashion (by code) I can achieve the result I need but I want to optimize the procedure using SQL features instead coding.
What I really need is a query that reports just a single row for each group of rows that matches condition1.
I need also to insert the count of the matching rows as a field of the result row.
I'm running MySQL 5.6 and code the application trough Visual Studio 2013 Pro coding in C# (using MySQL.Data package).
Best Regards,
WeirdGyn
All you need to do is GROUP BY the condition1 field. It would also appear that if you just need the COUNT by group that you wouldn't need the join.
SELECT condition1, COUNT(*)
FROM table1
--INNER JOIN table2
-- ON table1.id_sub = table2.id
GROUP BY condition1
Basically what I want to do can be put neatly in the following two steps.
Execute a query on a particular table. I then get the result set.
One of the columns in the result set say 'id' has only numbers in it. I would want to take this number
'id' in every row in the result set, join it with another table called ID_NAMES & replace the
individual ids of every row with corresponding names obtained by joining with ID_NAMES table.
In a sense this is like performing post SQL query on the result set I obtain from executing pre SQL query.
Is there anyway I can accomplish this?
SELECT table1.id, table2.name FROM table1 INNER JOIN table2 on table1.id = table2.id
Try this query....
Refer at JNevill
Hi i have this query but its giving me an error of Operand should contain 1 column(s) not sure why?
Select *,
(Select *
FROM InstrumentModel
WHERE InstrumentModel.InstrumentModelID=Instrument.InstrumentModelID)
FROM Instrument
according to your query you wanted to get data from instrument and instrumentModel table and in your case its expecting "from table name " after your select * .when the subselect query runs to get its result its not finding table instrument.InstrumentModelId inorder to fetch result from both the table by matching you can use join .or you can also select perticuler fields by tableName.fieldName and in where condition use your condition.
like :
select Instrument.x,InstrumentModel.y
from instrument,instrumentModel
where instrument.x=instrumentModel.y
You can use a join to select from 2 connected tables
select *
from Instrument i
join InstrumentModel m on m.InstrumentModelID = i.InstrumentModelID
When you use subqueries in the column list, they need to return exactly one value. You can read more in the documentation
as a user commented in the documentation, using subqueries like this can ruin your performance:
when the same subquery is used several times, mysql does not use this fact to optimize the query, so be careful not to run into performance problems.
example:
SELECT
col0,
(SELECT col1 FROM table1 WHERE table1.id = table0.id),
(SELECT col2 FROM table1 WHERE table1.id = table0.id)
FROM
table0
WHERE ...
the join of table0 with table1 is executed once for EACH subquery, leading to very bad performance for this kind of query.
Therefore you should rather join the tables, as described by the other answer.
Can anyone help me on how could I join two tables without merging the result into single row? Please see below query:
SELECT *
FROM resorderdetails rd
INNER JOIN resinvalidorderdetails ri
ON rd.itemid=ri.srcitemid;
Let say for example I have 1 row in resorderdetails table with field itemid=1 and I have 1 row in resinvalidorderdetails table with field srcitemid=1.
If we will going to execute the above query, it will return a result of single row [merging the data of two tables]
What I want to do is to have two rows as a result. The first row is the record came from resorderdetails and the other row is the record came from resinvalidorderdetails without using UNION ALL or UNION.
How could I do it? Is it possible?
It's not possible, because you wannna have :
SELECT * FROM resorderdetails rd
and
select * from resinvalidorderdetails
There are only joins operations and UNION which you don't want to use, and the JOINS will put together or remove commons ids and UNION will do exactly what you want
Beside to display a query with 2+ tables you need something in common(even the union need same number of columns).