transaction_jobs table
transaction_lines table
SQL executed :
SELECT
tj.id AS tj_id, tj.debit AS tj_debit, tj.credit AS tj_credit, job_id AS tj_job_id,
tl.id AS tl_id, tl.debit AS tl_debit, tl.credit AS tl_credit
FROM transaction_jobs tj
LEFT JOIN transaction_lines tl ON tl.account_id = tj.account_id AND tl.transaction_id = tj.transaction_id
WHERE tj.transaction_id = 198044 AND tj.account_id = 64375
Executed code result:
Result I expected:
Is there a way I could get the result I expected?
You have multiple records of the same transaction on both tables and are matching multiple times.
TJ ID 385610 will match once with TL ID 2277 and again with TL ID 2278. because both have transaction_id 198044.
If you wish for the results to be only one match, you need to use
GROUP BY tj_transaction_id
You can read more here:
https://dev.mysql.com/doc/refman/8.0/en/group-by-modifiers.html
Related
I have this query I'm trying to build to display specific information for a stored table. I'm needing the query to also display the Enemy Guild Name but I'm having trouble getting the query to take the Enemy Guild ID and link it to the name.
SELECT g.wPos as wPos, g.szGuildName as szGuildName, g.dwGuildExpWeek as dwGuildExpWeek, g.dwEnemyGuildID as dwEnemyGuildID, gm.wPower as wPower, gd.szName as szName
FROM guild as g
LEFT JOIN guild_member AS gm ON gm.dwGuildID = g.dwGuildID AND gm.wPower = '1'
LEFT JOIN gamedata AS gd ON gd.dwID = gm.dwRoleID
WHERE g.wPos = '1'
The output of the query right now results in the following:
Query Results Currently
What I need it to do now is take the dwEnemyGuildID it finds and then use that ID to search for the szGuildName while also displaying the other data it finds.
Use the concept of SELF JOIN, in which we will join same table again if we have a field which is a reference to the same table. Here dwEnemyGuildID is reference to the same table.
A trivial example of the same is finding Manager for an employee from employees table.
Reference: Find the employee id, name along with their manager_id and name
SELECT
g.wPos as wPos,
g.szGuildName as szGuildName,
g.dwGuildExpWeek as dwGuildExpWeek,
g.dwEnemyGuildID as dwEnemyGuildID,
enemy_g.szGuildName as szEnemyGuildName, -- pick name from self joined table
gm.wPower as wPower,
gd.szName as szName
FROM guild as g
LEFT JOIN guild_member AS gm ON gm.dwGuildID = g.dwGuildID AND gm.wPower = '1'
LEFT JOIN guild AS enemy_g ON g.dwEnemyGuildID = enemy_g.dwGuildID -- Use self join
LEFT JOIN gamedata AS gd ON gd.dwID = gm.dwRoleID
WHERE g.wPos = '1';
I have a table called Cats with two columns (shortId, LongId)
And another table called cat-to-cat-dependency that has two columns (sourceShortId, targetShortId)
I want to return a list of all the source Cat LongIds given a target Cat LongId.
My sql statement is
SELECT C.LongID
FROM CAT C INNER JOIN
CAT-TO-CAT-DEPENDENCY CD
ON CD.sourceShortId = CAT.shortId
WHERE CAT.LongID = '1234'
But this always just returns the value that I input.
Try this query:
select C1.LongId
from cat-to-cat-dependency CD
join Cats C1 on C1.shortId = CD.sourceShortId
join Cats C2 on C2.shortId = CD.targetShortId
where C2.LongId = '1234'
I am trying to create an inner-join MySQL Query of two tables:
'swatchset'
->swatchset_id
->swatchset_name
'swatches'
->swatch_id
->swatch_name
->swatch_hex
->etc...
I currently have one record in the 'swatchset' table:
swatchset_id | swatchset_name
8 default
The 'swatches' table is empty.
I want to get as result the 'swatchset'.'swatchset_name'
I guess I don't understand INNER JOIN very well because this Query results nothing:
SELECT `swatchset`.`swatchset_name`, `swatches`.`swatch_id`, `swatches`.`swatch_name`, `swatches`.`swatch_hex`, `swatches`.`swatch_type` FROM `swatchset`
INNER JOIN `swatches`
ON `swatches`.`f_swatchset_id` = `swatchset`.`swatchset_id`
WHERE `swatchset`.`swatchset_id` = '8';
How can I get at least the found 'swatchset' row as result?
Left outer join will fill with nulls columns of second table when not found:
SELECT `swatchset`.`swatchset_name`, `swatches`.`swatch_id`, `swatches`.`swatch_name`, `swatches`.`swatch_hex`, `swatches`.`swatch_type` FROM `swatchset`
LEFT OUTER JOIN `swatches`
ON `swatches`.`f_swatchset_id` = `swatchset`.`swatchset_id`
WHERE `swatchset`.`swatchset_id` = '8';
inner join doesn't return entries from first table without matching ones in second table
We have two tables in mysql database.Screenshots are attached below.
Given table ads_testTable
here is the screenshot of my dimesnionvalue_flattable
We have to run a query like the one below.
SELECT Quiz_Attempt.L1_Key dimID,
Quiz_Attempt.L1_Label CatVars,
COALESCE(**xyz**,0) AS series0
FROM DSQ_ADSSCHEMA.ADS_TestTable dataTable
RIGHT OUTER JOIN LS_CONFIG.DSQ_DIMENSIONVALUES_FLAT Quiz_Attempt on dataTable.Quiz_Attempt = Quiz_Attempt.L1_Key
WHERE Quiz_Attempt.L0_Key = 'All Levels' AND
Quiz_Attempt.DimensionID = 'Packet'
GROUP BY Quiz_Attempt.L1_Key, Quiz_Attempt.L1_Label;
My motive is to write a query in place of xyz so that I can get avg of obtainedMarks column in testtable according to the value of dimID I get.Each distinct Quiz_Attempt is a different test so If a Packet is repeating for a particular Quiz_Attempt in testTable, it should take only one value for that AttemptID.
I think you query could take the form of:
SELECT
L1_Key dimID,
L1_Label CatVars,
COALESCE('**xyz**',0) AS series0
FROM (
SELECT *
FROM (SELECT * FROM ADS_TestTable GROUP BY ADS_TestTable.Quiz_Attempt) dataTable
RIGHT OUTER JOIN DSQ_DIMENSIONVALUES_FLAT Quiz_Attempt on dataTable.Quiz_Attempt = Quiz_Attempt.L1_Key
WHERE Quiz_Attempt.L0_Key = 'All Levels' AND
Quiz_Attempt.DimensionID = 'Packet'
GROUP BY dataTable.Quiz_Attempt
) A GROUP BY dimID, CatVars;
The JOIN is done in an inner query, and grouped by Quiz_Attempt, so that you get a single row per attempt. This result is then used to compute what you need.
I have tow tables, the first is mda_alert_info and the second is key_contacts_info. For each alert set up there may be multiple corresponding contacts. Both tables are linked by 3 columns mda_id, stage_id and ref_number and during the query I will have to pass figures values for them
How do I get all what I want from both tables in one statement. Below are the individual SELECT statements.
$result = mysql_query("SELECT `mda_name`, `project_name`, `ipc_id` FROM `mda_alert_info` WHERE `stage_id`=1 AND `mda_id`=2 AND `ref_number`= '444'");
This will always return one record
$result = mysql_query("SELECT `contact_role`, `contact_email`, `contact_ph_number` FROM `key_contacts_info` WHERE `stage_id`=1 AND `mda_id`=2 AND `role`=0 AND `ref_number`='444'");
This may return multiple records
I tried for a while and couldn't get it to work so I tried adding another field called 'me' to both tables which is basically concatting mda_id and stage_id strings then I tried the query below.
$result = mysql_query("SELECT key_contacts_info.contact_role, key_contacts_info.contact_email, key_contacts_info.contact_ph_number, mda_alert_info.mda_name, mda_alert_info.project_name, mda_alert_info.ipc_id FROM key_contacts_info LEFT JOIN mda_alert_info ON key_contacts_info.me = mda_alert_info.me WHERE stage_id=1 AND mda_id=2 AND role=0 AND ref_number='444'");
But it's still not working. What am I doing wrong and how do I get it to work.
UPDATE:
Sorry but I should clarify something on the relationship between the tables the three columns exist on each table but are not primary keys on either of the tables
SELECT a.*, b.*
FROM mda_alert_info a
INNER JOIN key_contacts_info b
ON a.mda_id = b.mda_id AND
a.stage_id = b.stage_id AND
a.ref_number = b.ref_number
WHERE b.role = 0 AND
a.stage_id = 1 AND
a.mda_id = 2 AND
a.ref_number = '444'
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
select
a.mda_name,
a.project_name,
a.lpc_id,
k.contact_role,
k.contact_email,
k.contact_ph_number
from mda_alert_info a
join key_contacts_info k
on a.state_id = k.state_id
and a.mda_id = k.mda_id
and a.ref_number = k.ref_number
where a.state_id = 1
and a.mda_id = 2
and a.ref_number = '444'
and k.role = 0
;