Lookup/Link two MySQL tables - php

I'm fairly new to SQL and have been trying to figure this one out for a while.
I have tableA with Club_Name, Image_Path,... and then I have tableB with Club_Name, Article,...
I am exporting tableB to a JSON array and need to include Image_Path, how can I best do this? If I ad an Image_Path column to tableB is there a means to conditionally populate it based on Club_Name and a lookup to tableA?

You need to join the 2 tables together, based on a column value that exists in both tables.. ..in your case: 'Club_Name'
SELECT a.Club_Name, a.Image_Path, b.Article
FROM tableA a
JOIN tableB b USING (Club_Name)

Related

Make a table from from table A and table B where match

MySQL noob here. I need to create a product catalog table from two others. Table A has a product weight and Table B has description. So I need to make a Table C with the weight and description of course.
These catalogs are different, from different sources, and the only field I can match is SKU.
Should I make Table C from a copy of Table A (with weight), then add a description column, then:
update tableC
join tableA on tableB.sku = tableA.sku
set description = tableB.description
Creating another table to copy and hold values related to each other from two other tables is a poor approach. SQL is a relational language; you should leverage that to simply get both values from both tables:
SELECT
tableA.weight
, tableB.description
FROM tableA
LEFT JOIN tableB on tableB.sku = tableA.sku
No need to create a copy to relate the two in a whole other table. That's unnecessary complexity.
Maybe
INSERT INTO
TableC (`weight`,`desc`)
SELECT
TableA.weight, TableB.desc
FROM
TableA
INNER JOIN
TableB ON TableA.id = TableB.id;
?
(see http://dev.mysql.com/doc/refman/5.0/en/insert-select.html)

How to copy data from one table to another in MySQL?

What I'm trying to do should be really simple. I have two tables with the following columns:
Table 1:
Name, Level
Table 2:
Name, Cost
Name is the primary key. I want to combine both table's data into one table that has all three columns. What I've been trying to do is add a Cost column into Table 1 and copy all the Cost values from Table 2 into it. I've tried numerous suggestions from other threads on this site and I've never had one work for me. The new Cost column in Table 1 never budged with any new values. Why?
I am doing this on MySQL Workbench on Ubuntu.
Here's one that I tried using (New cost column already made for Tbl1):
UPDATE Tbl1
SET Cost = (
SELECT Cost
FROM Tbl2
WHERE Name = 'SpecificName')
WHERE Name = 'SpecificName;
This works when I specify individual rows but it doesn't work when I replace Name = 'SpecificName' with something like "Tbl1.Name = Tbl2.Name"
The problem you face is when some names are in one table but not the other. Here is one method, using a single create table as:
create table NameLevelCost as
select n.name, t1.level, t2.cost
from (select name from table1
union
select name from table2
) n left join
table1 t1
on t1.name = n.name left join
table2 t2
on t2.name = n.name;
This assumes that name is unique in each of the tables.
update Tbl1 t1 left join Tbl2 t2 on t2.Name = t1.Name set t1.Cost = t2.Cost;

Compare two large table for unique record

I have two table where tableA contains 1 million records while other tableB contains 13 million records , Now I want record from tableA those are not in tableB
I used below query
SELECT tableA .mergedSSN,total_unique_ssn.unique_ssn FROM `tableA`,`tableB` WHERE tableA.mergedSSN <> tableB.unique_ssn
But with above query It is taking too long time ,Please help me to get record very fast.
First of, create index by columns in each table (if you have not them yet). Then,
SELECT
tableA.mergedSSN
FROM
tableA
LEFT JOIN
tableB ON tableA.mergedSSN=tableB.unique_ssn
WHERE
tableB.unique_ssn IS NULL
How about
SELECT tableA.unique_ssn FROM tableA
WHERE tableA.unique_ssn NOT IN (SELECT tableB.unique_ssn FROM tableB)
You'd need to ensure that you have both tableA.unique_ssn and tableB.unique_ssn as indexed fields.

mysql query to select data from table2 which does not exist in table1

I have 2 tables table1 and table2, where table1 contains the data collected so far, and table2 contains table1's data along with some updated/additional data. I need to retrieve only this updated/additional data and insert it into table1.
Now I know I can use NOT IN to do this, but I am not sure it will be a very efficient solution in case of a huge number of records. Any suggestion on what the best approach would be in terms of execution speed?
This can be done with simple join both tables
something like below:
select t1.* from table1 as t1 join table2 as t2 on t1.id=t2.id where ...[]
I'm not sure if i've understand your question correctly but let me give it a try. Suppose you have a design like this:
TableA : {colA, colB, colC, colD, colE}
TableB : {colA, colB, RecC, RecD, RecE}
where Tables (tableA, tableB) is joined on ColA. I Assumed that TableA's columns (colC, ColD, colE) will be updated and the records are based on TableB's columns (recC, recD, recE).
In your statement: I need to retrieve only this updated/additional data and insert it into table1.. I think you want to update TableA's records based on TableB
UPDATE TableA a INNER JOIN TableB b ON a.ColA = b.ColA
SET a.ColC = b.RecC,
a.ColD = b.RecD,
a.ColE = b.RecE
-- WHERE (Condition) -- if you want to have a condition.
so the statement above updates all the records in tableA if colA also exist in tableB since I've used INNER JOIN. You could also use LEFT JOIN.

There are two columns in the new table(table1 join table2) which have the same column name, how to get the values of both respectively

select * from table1 join table2 on table1.column3=table2.column4 where ...
...
$row=mysql_fetch_assoc($result);
However, there are two columns in the new table(table1 join table2) which have the same column name, how to get the values of both respectively?
Call the columsn out specifically with an alias like
SELECT table_1.id as table_1_id, table_2.id as table_2_id
Youll have to list out all or most of the columns form at least one of the tables this way but you can get access to cols with the same name across multiple tables.
Prefix the column name in the select with its table name.
select table1.my_column, table2.my_column
from table1, table2
where table1.id = table2.t1_id
But with this method, you would have to read the columns using their returned order indexes, rather than their names. Another answer mentioned using as to name each column, so consider that if you're going to read them by name.
When there's a column name collision due to a query joining 2+ tables, you can not use *. You can use:
SELECT table_1.*,
table_2.*
FROM table_1,
table_2
If that doesn't return the list of columns you want, you will have to explicitly list every column. There's no way around - it's an all or nothing deal.
Table Aliases
...but typing out the full table name every time can be a pain, which is why you can alias tables:
SELECT t1.*,
t2.*
FROM table_1 AS t1,
table_2 t2
Either alias notation is supported. They're also required if you want to join a table to itself.
Using only the column names in a Select list that you actually need\ is always the best, but when you want everything, then, well, go ahead and use the *:
Select a.*,
b.*,
a.id as a_id,
b.id as b_id,
a.name as a_name,
b.name as b_name
from tablea a,
tableb b
...
It won't hurt to be redundant, as a.* includes a_id and a_name, but there values from the * get lost in the associative array, so just put them back in with new, unique names.

Categories