12

Merge 2 simple queries to a query

 2 years ago
source link: https://www.codesd.com/item/merge-2-simple-queries-to-a-query.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Merge 2 simple queries to a query

advertisements

I have a simple senario where I am executind 2 queries first to get an ID from a table

And then using this ID to access information from second table.

SELECT ID
FROM TEST_1
WHERE name = 'Example 1'

Select *
FROM TEST_2
WHERE Parent_ID = %ID retrived from the above query%


Use IN (if subquery returns more than 1 ID) or = (if subquery returns only 1 ID):

Select *
FROM TEST_2
WHERE Parent_ID = (SELECT ID FROM TEST_1 WHERE name = 'Example 1')

Related Articles

can I merge these 2 queries into a query

hello can i merge those 2 queries in one query my first query get the number of articles in database and second query get the sum of all visits of all article whats the best method to make it one query $stmt = $db->query('SELECT * FROM stories'); $st

Merge these two queries into one query

I have the following queries: SELECT Sites.EDISID, Sites.[Name], (SUM(DLData.Quantity) / 8) AS TiedDispense FROM Sites JOIN UserSites ON UserSites.EDISID = Sites.EDISID JOIN Users ON Users.[ID] = UserSites.UserID JOIN MasterDates ON MasterDates.EDISI

Is there a way to merge these two simple queries

I'm sure I'm ridiculous for thinking it's possible but is there a way to merge these two queries? Query #1: select distinct count(l.lease_id) as 'Count_Transactions_ALL', sum(l.net_area) as 'Total_Area_ALL' from lease_deal.lease l where l.deal_approv

How to merge different MySQL queries with values ​​defined in simple query in Java?

I want to merge four different SQL queries into single query in order to reduce usage of SQL connections This is my code: long WTPty = 0L; // holds some value from other part of program long NTPty = 0L; // holds some value from other part of program

Can I merge 2 different queries (by returning a single record) into a single query, returning a single record?

I am not so into databse and I have the following doubt. I am using MySql. I have two queries that both returns a single record. Something like this: QUERY 1: select id_1, field_1.1, field_1.2,............., field_1.n where id=1; Returning a single r

A complex query vs several simple queries

What is actually better? Having classes with complex queries responsible to load for instance nested objects? Or classes with simple queries responsible to load simple objects? With complex queries you have to go less to database but the class will h

Merge 2 sql queries into one

I have two sql queries which when run independent produces the correct results Query 1 SELECT id, (6371 * acos( cos( radians(9.977364864079215) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(76.58620953448485) ) + sin( radians(9

How to merge these two queries into one?

Quite simply... How can I merge these two queries into one? $sql = "UPDATE `data` SET `views` = `views` + 1 WHERE `id`=$id AND `source`='$source' AND `keyword`='$keyword'"; $sql = "UPDATE `settings` SET `count` = `count` + 1 WHERE `id`=$id&

Collecting two queries in a query

Can i collect these two queries in one query ? INSERT INTO [dbo].[acceuil](libelle,value,categorie,param) SELECT 'Scom',count(*),'event','month1' FROM [dbo].[full] WHERE date_reception BETWEEN @StartDate AND @EndDate and event_class LIKE '%MOM%' INSE

How to combine these 2 queries in a query without using the union

SELECT b.KPCNO ,b.KPC_FULL_NAME FROM xxkpc_hr_personnel_v2 b WHERE b.type(+) = 'KPC Employee' AND b.DESIGNATION is null AND b.kpcno IS NOT NULL AND b.ORGANIZATION_ID=(select g.ORGANIZATION_ID from xxkpc_fn_web_personnel_v g where g.kpcno = :kpcno) GR

On the two sql queries below, suggest which one is the best. Single query with join or two simple queries?

Assuming result of first query in A) (envelopecontrolnumber,partnerid,docfileid) = (000000400, 31,35) A) select envelopecontrolnumber, partnerid, docfileid from envelopeheader where envelopeid ='LT01ENV1107010000050'; select count(*) from envelopehea

Sql insert- 1 large query, or multiple simple queries

I am using PHP with an odbc connection to MSSQL database. Currently, I having around 1900 insert statements, in the one string, separated by a semicolon, and running that in 1 odbc_execute statement. Firstly, is this a bad method? Should I be process

Merge 2 mysql queries into one with the JOIN + OR statement

Code is self-explanatory How can i merge these 2 SQL queries into one ? something like "JOIN..ON.. OR shop_tags.parent_tag_id = shop_tags_link.tag_id" // query PARENT tags with this Design ID $list_tags_link_query = "SELECT * FROM shop_tags

Simple Relational Database - Efficient Query

This is essentially very simple I've just been rather verbose about it. My data is structured as shown at the bottom of my post. My goal is to organise the data in a simple spreadsheet format, 'flattening' the relational database. Something like: Pro

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK