Join two tables to one - Sql - mysql

SELECT
id, mapid, life_type, lifeid, x_pos, y_pos, foothold, min_click_pos, max_click_pos, respawn_time, life.flags, script.script
FROM
map_life life
LEFT JOIN
scripts script
ON
script.objectid = life.lifeid
AND
script.script_type = 'npc'
AND
helper = 0
LEFT JOIN
npc_data n
ON
n.npcid = life.lifeid
AND script.script_type = 'npc'
I'm trying to execute the following scripts. Basically, I'm showing all the rows from the table map_life, and also left joining column script from table scripts and column storage_cost from table npc_data if they lifeid column's value match scripts's objectid and npc_data's npcid.
However, it doesn't work properly. Why's that? I can't see the correct values for storage_cost.
Thanks

Your query is missing the [storage_cost] column in the returned data set:
SELECT
life.[id],
life.[mapid],
life.[life_type],
life.[lifeid],
life.[x_pos],
life.[y_pos],
life.[foothold],
life.[min_click_pos],
life.[max_click_pos],
life.[respawn_time],
life.[flags],
script.[script],
npc.[storage_cost]
FROM
[map_life] AS life
LEFT OUTER JOIN [scripts] AS script
ON ( life.[lifeid] = script.[objectid] )
LEFT OUTER JOIN [npc_data] AS npc
ON ( life.[lifeid] = npc.[npcid] )
WHERE
script.[script_type] = 'npc'
AND [helper] = 0
Also, it wasn't clear what table the [helper] column exists in. If the column is in the [scripts] table, then you could modify the query above by changing [helper] = 0 in the WHERE Clause to instead be script.[helper] = 0.
Added:
Naturally, since you are using LEFT OUTER JOINs, the returned values for script.[script] and npc.[storage_cost] may be NULL.
I hope this helps.

Related

MYSQL - join 2 tables, match multiple columns - update value from other table where multiple values match

I need to join two tables (TableCorrected) with (TableOriginal). Both tables have columns for ID-number and Articlenumber. Both tables also have a column named "Quantity".
I want to join the two tables, match ID-number AND Articlenumber and update the Quantity-value from "TableOriginal" to "TableCorrected" ONLY where ID-number as well as Articlenumber matches.
I've started a statement as below - but I'm sure it's not correct, returns 0 result.
UPDATE TableOriginal
INNER JOIN TableCorrected ON TableOriginal.ID = TableCorrected.ID
SET TableOriginal.Quantity = TableCorrected.Quantity
WHERE TableCorrected.ID = TableOriginal.ID
AND TableCorrected.Article = TableOriginal.Article
but I'm sure it's not correct
It is correct. But not optimal. More clear is, for example,
UPDATE TableOriginal
INNER JOIN TableCorrected USING (ID, Article)
SET TableOriginal.Quantity = TableCorrected.Quantity;
or
UPDATE TableOriginal
INNER JOIN TableCorrected ON TableOriginal.ID = TableCorrected.ID
AND TableOriginal.Article = TableCorrected.Article
SET TableOriginal.Quantity = TableCorrected.Quantity;
returns 0 result.
UPDATE does not return rows. Rather than SELECT.

How do I add a criteria to a query on one table but keep all the records from another?

I have the following query:
SELECT games_atp.ID1_G, odds_atp.K1
FROM games_atp LEFT JOIN odds_atp ON (games_atp.ID1_G = odds_atp.ID1_O) AND (games_atp.ID2_G = odds_atp.ID2_O) AND (games_atp.ID_T_G = odds_atp.ID_T_O) AND (games_atp.ID_R_G = odds_atp.ID_R_O)
I know the joining is convoluted but the original db is built without a primary key. The above works fine and importantly pulls all the records from games_atp. I now want to add a criteria into this to pull only certain K1 records from odds_atp. I added a WHERE clause as follows:
SELECT games_atp.ID1_G, odds_atp.K1
FROM games_atp LEFT JOIN odds_atp ON (games_atp.ID1_G = odds_atp.ID1_O) AND (games_atp.ID2_G = odds_atp.ID2_O) AND (games_atp.ID_T_G = odds_atp.ID_T_O) AND (games_atp.ID_R_G = odds_atp.ID_R_O)
WHERE (((odds_atp.ID_B_O)=2));
However, this overides the left join and only pulls records from games_atp where there is a corresponding record in odds_atp with ID_B_O = 2. How do I keep the criteria and all the records in games_atp? Thanks in advance.
Your current where condition will filter your final result, hence you are only seeing id_B_O = 2.
However, you could also add the wehre condition directly into your left join.
something like this.
SELECT
games_atp.ID1_G, odds_atp.K1
FROM
games_atp
LEFT JOIN odds_atp ON
(
(odds_atp.ID_B_O =2)
AND
(
(games_atp.ID1_G = odds_atp.ID1_O)
AND (games_atp.ID2_G = odds_atp.ID2_O)
AND (games_atp.ID_T_G = odds_atp.ID_T_O)
AND (games_atp.ID_R_G = odds_atp.ID_R_O)
)
);
or you could also take advantage of sub-queries

Select only distinct values for a particular column mysql

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.

Update data from one table to another

I have a small problem updating rows of one table with data from another one, please help.
Table l with columns
Make
Model
OEMNumberLatest
OemNumberPrevious
cStockCode
cDescription
The two columns cStockCode,cDescription are blank at the moment and are waiting to be populated, and the second table c with columns StockCode and Description
The question is how to move/copy contents of table c into rows of table l?
Where c.StockCode = l.OEMNumberLatest and or c.StockCode = l.OemNumberPrevious
You probably shouldn't move the contents of one table into the other. It is really better to just get the values when you need them, using a join:
select l.*, c.cStockCode, c.cDescription
from table1 l join
c
on c.StockCode = l.OEMNumberLatest or c.StockCode = l.OemNumberPrevious;
This seems like a strange condition, with the or because you can get multiple matches.
In any case, you can convert this to an update easily:
update table1 l join
c
on c.StockCode = l.OEMNumberLatest or c.StockCode = l.OemNumberPrevious
set l.cStockCode = c.cStockCode,
l.cDescription = c.cDescription;
When both conditions match, then one will arbitrarily be used for the update.
This is a example of a update syntax using another table
UPDATE tableL SET
tableL.cStockCode=tableC.StockCode,tableL.cDescription=tableC.Description
FROM tableL
JOIN tableC ON
(tableC.StockCode = tableL.OEMNumberLatest)
OR
(tableC.StockCode = tableL.OemNumberPrevious);
I assument that the contents in tableC is to be filled in tableL , and the JOIN is based on StockCode with OEMNumberLatest or OemNumberPrevious
Try this (save your data before trying, I am not sure of your database structure) :
UPDATE l
SET l.cStockCode = c.StockCode, l.cDescription = c.Description
FROM l
INNER JOIN c ON (c.StockCode = l.OEMNumberLatest OR c.StockCode = l.OemNumberPrevious)

MySQL Join tables, if criteria not met still return as null

I have this MySQL query
SELECT perms.permission_id, perms.long_title, perms.category, gperms.value FROM
acl_permissions AS perms JOIN acl_group_permissions AS gperms ON
(perms.permission_id = gperms.permission_id AND gperms.group_id = 1) WHERE
perms.type IN ('adm', 'cs') AND perms.simple_title NOT IN ('adm_', 'cs_')
ORDER BY perms.long_title
Basically the bit causing problems is in the ON. I want the query to return all rows from perms based on the where criteria regardless of whether there is a row matching gperms.group_id = 1.
gperms.value would ideally just be an empty string if there was no row matching gperms.group_id = 1.
You want to LEFT OUTER JOIN (or LEFT JOIN for short) gperms instead of just a straight join
SELECT perms.permission_id, perms.long_title, perms.category, gperms.value
FROM acl_permissions AS perms
LEFT JOIN acl_group_permissions AS gperms
ON (perms.permission_id = gperms.permission_id AND gperms.group_id = 1)
WHERE perms.type IN ('adm', 'cs')
AND perms.simple_title NOT IN ('adm_', 'cs_')
ORDER BY perms.long_title
which will return NULL for gperms.value for any row that doesn't join the gperms on the given condition.
If you want an empty string instead of the NULL you can use IFNULL():
SELECT perms.permission_id, perms.long_title, perms.category, IFNULL(gperms.value,'')