Retrieve data from another table - mysql

I have a table like
tbl_scripts
script_unique_id | system_id | script_name
+-----------------------------------------+
12345 | 89784 | Demo
And another table goes as
tbl_allowed_group_ids
system_id | script_unique_id |allowed_group_id
+---------------------------------------------+
89784 12345 56987
So now what I want is the row from tbl_scripts with group_id only if the unique id is in allowed_group_id
What I tried is this but is nowhere close to it....I know this is completely wrong
SELECT script_name FROM tbl_scripts WHERE script_unique_id = allowed_group_id

You will want to JOIN the tables:
select *
from tbl_scripts s
left join tbl_allowed_group_ids g
on s.system_id = g.system_id
and s.script_unique_id = g.script_unique_id
See SQL Fiddle with Demo
If you need help learning join syntax, here is a great visual explanation of joins.
A LEFT JOIN will return all rows in the tbl_scripts table regardless of whether or not it has a matching row in the tbl_allowed_group_ids table. If you only want matching rows, then you can use an INNER JOIN

JOIN the two tables:
SELECT t1.script_name
FROM tbl_scripts t1
INNER JOIN tbl_allowed_group_ids t2 ON t1.script_unique_id = t2.allowed_group_id

Related

SQL Distinct based on different colum

I have problem to distinct values on column based on other column. The case study is:
Table: List
well | wbore | op|
------------------
wella|wbore_a|op_a|
wella|wbore_a|op_b|
wella|wbore_a|op_b|
wella|wbore_b|op_c|
wella|wbore_b|op_c|
wellb|wbore_g|op_t|
wellb|wbore_g|op_t|
wellb|wbore_h|op_k|
So, I want the output to be appear in different field/column like:
well | total_wbore | total_op
----------------------------
wella | 2 | 3
---------------------------
wellb | 2 | 2
the real study case come from different table but to simplify it I just assume this case happened in 1 table.
The sql query that I tried:
SELECT well.well_name, wellbore.wellbore_name, operation.operation_name, COUNT(*)
FROM well
INNER JOIN wellbore ON wellbore.well_uid = well.well_uid
INNER JOIN operation ON wellbore.well_uid = operation.well_uid
GROUP BY well.well_name,wellbore.wellbore_name
HAVING COUNT(*) > 1
But this query is to calculate the duplicate row which not meet the requirement. Anyone can help?
you need to use count distinct
SELECT
count(distinct wellbore.wellbore_name) as total_wbore
count(distinct operation.operation_name) as total_op
FROM well
INNER JOIN wellbore ON wellbore.well_uid = well.well_uid
INNER JOIN operation ON wellbore.well_uid = operation.well_uid
Final query:
SELECT
well.well_name,
COUNT(DISTINCT wellbore.wellbore_name) AS total_wbore,
COUNT(DISTINCT operation.operation_name) AS total_op
FROM well
INNER JOIN wellbore ON wellbore.well_uid = well.well_uid
INNER JOIN operation ON wellbore.well_uid = operation.well_uid
GROUP BY well.well_name

Unique rows in join result

I have a tables of delas and curencies look like this
curecnies
id,code
pairs (the available pairs of curencies )
id to_sell to_buy
deals
id
user_id
pair_id
amount_to_sell
amount_to_buy
So I need to get all match deals which can execute , but I am can not get the unique matches.
Here is my sql query
select *
from deals as d1
join deals d2
on d1.sell_amount = d2.buy_amount and d1.buy_amount = d2.sell_amount
i am getting result look like this
id | user_id | pair_id | amount_to_buy | amount_to_sell | id | user_id | pair_id | amount_to_buy | amount_to_sell
1|2|1|1000|3000|2|1|2|3000|1000
2|1|2|3000|1000|1|2|1|1000|3000
You may try using a least/greatest trick here:
SELECT t1.*, t2.*
FROM
(
SELECT DISTINCT
LEAST(d1.id, d2.id) AS d1_id,
GREATEST(d1.id, d2.id) AS d2_id
FROM deals AS d1
INNER JOIN deals d2
ON d1.sell_amount = d2.buy_amount AND
d1.buy_amount = d2.sell_amount
) d
INNER JOIN deals t1
ON d.d1_id = t1.id
INNER JOIN deals t2
ON d.d2_id = t2.id;
The basic idea here is that the subquery labelled d finds a single pair of matched deal IDs, using a least/greatest trick. Then, we join twice to the deals table again to bring in the full information for each member of that deal pair.

Join Two Tables and Replace Values Where Conditions Meet

I'm trying to come with an expression to join two tables (the first two) to get something like the third one.
I want to SELECT 'Sitepage' and 'Medium' and JOIN the first two tables ON the rows where the RIGHT 5 characters are matching between 'Sitepage' and 'Campaign ID'. Additionally, IF there is a match, THEN 'Program' will REPLACE 'Medium'. What would be the Syntax?
Sitepage | Medium
xyz.com/campaign=12345 | A
xyz.com/campaign=23456 | C
Campaign ID | Program
12345 | B
Sitepage | Medium
xyz.com/campaign=12345 | B
xyz.com/campaign=23456 | C
http://i.stack.imgur.com/pq35n.png
I based my answer off of #Juan's, but I had to make some adjustments to get it to work.
SELECT
SitePage, COALESCE(t2.Program, t1.Medium) as Medium
FROM Table1 t1
LEFT JOIN Table2 t2
ON RIGHT(t1.Sitepage, 5) = COALESCE(t2.`Campaign ID`, -1);
#Abhik was heading in the right direction too. It's more generic than the one above, which assumes that the last 5 characters of SitePage will be the only pertinent ones. With that said, I would have gone with...
SELECT
SitePage, COALESCE(t2.Program, t1.Medium) as Medium
FROM Table1 t1
LEFT JOIN Table2 t2
ON SUBSTRING_INDEX(t1.Sitepage,'=',-1)
= COALESCE(t2.`Campaign ID`, -1);
SQL Fiddle example
You can use substring_index and then update by join
update table1 t1
join table2 t2 on t2.`Campaign ID` = substring_index(t1.`Sitepage`,'=',-1)
set t1.`Medium` = t2.`Program`
SELECT SitePage, ISNULL(t2.Program, t1.Medium) as Medium
FROM Table1 t1
LEFT JOIN Table2 t2 ON RIGHT(t1.Medium, 5) = t2.CampaignId
That should do the trick. Left Join on the expresion you want, when there is a match, Program will not be null so you can pick it up, otherwise pick medium. If your table2 allows null on Program you may need to tweak this.

Select Data from two tables where ID on both tables are same

Okay I have two tables called subobject: parentID, objectName, subID(primary) and subrelation: ID, className
parentID | objectName | subID ID| className|
_____________________________ ______________
84 | Test | 14 14| BOM
84 | Test2 | 15 15| Schematics
I want to match SubID with ID from both tables depending if they are the same values, then iterate all the values that are the same. Whats the query to do this in Mysql.
this is how I want it to look:
subobjectNAME:
--RelatedClass
--RelatedClass2
etc.
I know this is has something to do with JOIN and this is the mysql Query im using but its not working
"SELECT * from subrelation inner join subobject on subrelation.ID = subobject.subID"
also my while loop to grab this
while($join = mysqli_fetch_assoc($join))
JOIN the two tables:
SELECT
so.objectName,
sr.ClassName
FROM subobject AS so
INNER JOIN subrelation AS sr ON so.subId = sr.ID;
See it in action here:
SQL Fiddle Demo
Also, see the following post for more info about the different types of JOINs:
A Visual Explanation of SQL Joins.
select
a.objectName, b.className
from
subobject a
left join
subrelation b on a.subID = b.ID
Use a Join
SELECT
subobject.ObjectName,
subrelation.ClassName
FROM
subobject
INNER JOIN
subrelation ON subobject.subID = subrelation.ID
You can find information on SQL Joins here:
http://en.wikipedia.org/wiki/Join_(SQL)
And information from the MySQL manual on Joins:
http://dev.mysql.com/doc/refman/5.0/en/join.html

How do I do 2 unique LEFT JOINs on the same table cell?

In mysql I'd like to do 2 unique LEFT JOINs on the same table cell.
I have two tables.
One table lists individual clients and has a clientNoteID and staffNoteID entry for each client. clientNoteID and staffNoteID are both integer references of a unique noteID for the note store in the notesTable.
clientsTable:
clientID | clientName | clientNoteID | staffNoteID
notesTable:
noteID | note
I'd like to be able to select out of the notesTable both the note referenced by the clientNoteID and the note referenced by the staffNoteID.
I don't see any way to alias a left join like:
SELECT FROM clientsTable clientsTable.clientID, clientsTable.clientName, clientsTable.clientNoteID, clientsTable.stylistNoteID
LEFT JOIN notes on clientTable.clientNotesID = notes.noteID
LEFT JOIN notes on clientTable.staffNoteID = notes.noteID as staffNote
(not that i think that really makes too much sense)
So, how could I query so that I can print out at the end:
clientName | clientNote | staffNote
When you join a table the alas must be immediately after the table name, not after the join condition. Try this instead:
SELECT clientsTable.clientName, n1.note AS clientNote, n2.note AS staffNote
FROM clientsTable
LEFT JOIN notes AS n1 ON clientTable.clientNotesID = n1.noteID
LEFT JOIN notes AS n2 ON clientTable.staffNoteID = n2.noteID
you need to alias the tables themselves
SELECT FROM clientsTable clientsTable.clientID, clientsTable.clientName, clientsTable.clientNoteID, clientsTable.stylistNoteID
LEFT JOIN notes a on clientTable.clientNotesID = a.noteID
LEFT JOIN notes b on clientTable.staffNoteID = b.noteID
SELECT CT.clientName, N1.note AS clientNote, N2.note AS staffNote
FROM clientsTable CT
LEFT JOIN notes N1 on CT.clientNotesID = N1.noteID
LEFT JOIN notes N2 on CT.staffNoteID = N2.noteID