There are two tables table1 and table2
table1 has got two columns name and rank
table2 has got only one column name
names in table2 are almost listed in table1
I want compare two table and pull rank info from table1 and update/alter table2 with rank
table1
name | rank
-------------
john | 2
mathews| 5
keyn | 4
emly | 25
yancy | 8
stewart| 9
kim | 12
chris | 19
table2
name
-------
john
mathews
keyn
emly
yancy
stewart
I want update/insert rank details to table2 from table1
thats it and sorry for the confusion
Seem like you want to do something like this:
update table2,table1 set table2.rank=table1.rank where table2.name=table1.name
This will update the second table with ranks from the first table where the names are equal.
Then put auto increament field of table one in table2. and after this apply left join using these id and pull the info
refer this link
http://www.wellho.net/solutions/mysql-left-joins-to-link-three-or-more-tables.html
Related
Is it possible to join 2 tables together on the basis of table 1 column value equaling table_2 column name?
table_1 table_2
----- -------------
| A | | 1 | 2 | 3 |
|---| |-----------|
| 1 | | Q | W | E |
----- -------------
Desired result is:
Select A from table_1 join table_2 WHERE table_1 A = table_2 column name.
Result:
1Q
I am using PHP, sorry for the poor psudo SQL, I honestly have no idea how to word it. Thank you for any help you can offer!
You want SQL where you use a value from one column as the name of another column. You Can't Do Thatâ„¢. In pure SQL queries the names of tables and columns must be literals (using the lingo of php style languages.
You probably can get the result you need by reading your tables into RAM with your php program, then programming your table-to-table correlation.
SELECT CONCAT(table_1.A,"",table_2.1) as RESULT
FROM table_1
LEFT JOIN table_2 ON table_1.A = table_2.1
I have 2 tables like this:
Table1
id | name
------------------
1 | David
2 | Lucas
3 | Antonio
Table2
id | name
------------------
1 | Sergio
2 | Sergio
3 | Lucas
I want to select data to group duplicate records and return this:
name
------------------
David
Lucas
Antonio
Sergio
So I tried with this query
SELECT name FROM Table1 JOIN Table2 GROUP BY name
But nothing is returned.
You need UNION instead of JOIN :
SELECT name
FROM table1
UNION
SELECT name
FROM table2;
JOIN is used for matching rows and produce subsequent columns from joined tables while union will combine all rows from two or more tables.
use union
select name fron table1
union
select name from table2
I have 2 tables and I can't merge the values of these tables into table2.
Table1:
id | studnum | fname | lname | mname
1 | 1001 | Mark | Lei | Ramos
Table2:
id | studnum | remarks
Is it possible that when I input the values into Table2, the studnum from Table1 will input into Table2 also?
I tried this but it doesn't worked.
$sql = "SELECT table1.studnum FROM table1, table2
WHERE table1.studnum = table2.studnum";
So that the Table2 will be like this output
id | studnum | remarks
1 | 1001 | good
What you're looking for is using a JOIN Statement.
If the shared key is between them is studnum, you can do
SELECT * FROM table1
JOIN table2
ON table1.studnum = table2.studnum
Try a MySQL join function. Example:
SELECT *
FROM table1
JOIN table2
ON table1.studnum=table2.studnum;
More reading on joins: http://www.keithjbrown.co.uk/vworks/mysql/mysql_p5.php
If those are exactly your tables you dont really need what you are requesting as it goes against normalization.
In the case you will only have one or none remarks for an entry in table 1 there is no need for table 2, the remarks should be in table 1.
In the case you need multiple remarks for a single entry in table 1, you dont need stdnum in table 2. If your problem in this scenario is to construct a query to get the remakrs based on stdnums you can easily achieve this with a joint.
The only field you should be duplicating from table to table for a record/object is the id. You can (and may be should) scrifice normalization in pro of efficiency but your example is not the case.
I want to save multiple rows with single id store in table. What should I do? Please guide me.
For example:
| table1 |
|-----------------|
|id |name |
|001(pk) |Ajit |
| table2 |
|-----------------|
|id(FK) |address |
|001 |Pune |
|001 |Mumbai. |
means TWO TABLES are their in table1 id is primary key & table2 id is foreign key
i.e.: 001 id should multiple address it would be save into table2 but id must be same,
i.e.: address textbox will generate at runtime
Put its(id) default value as "001"(you deserve it) using mysql query and then while inserting a new entry leave that column to add as new.
If you're wanting to link the data in the two tables (i.e. a users table and an addresses table) just use a foreign key.
Your users table:
id | name
================
1 | Martin Bean
Your addresses table:
id | user_id | address
==================================
1 1 Newcastle upon Tyne
This way, the primary key of your addresses table is not dependent on the ID of the users table. And your query to fetch both pieces of data is easy with a join:
SELECT
u.id,
u.name,
a.address
FROM
users u
LEFT JOIN
addresses a ON u.id = a.user_id
I have two tables, one table is called queuelist and the other is call info. In the queuelist table it just lists different IDs. I am trying to get the 'clientID' from that table and match it with the 'ID' in the other table that contains all of the info and display it back on the page. Here is how the tables look:
Table - queuelist
ID | clientID
-------------
1 | 589
2 | 254
3 | 486
Table - info
ID | Name | Phone
--------------------
256 | Bob | 5551231234
486 | Jack | 5551231234
589 | Jill | 5551231234
This is what they call joining tables, you should use a query like this:
SELECT i.ID, i.Name, i.Phone FROM `queuelist` AS q
LEFT JOIN `info` AS i ON (
q.clientID = i.ID
);
I'm using aliases for shorter notation in the above query (queuelist becomes q and info becomes i) and then set the join condition (the bit between the ON()) to be the clientID from the queuelist table should match the ID in the info table.
Also see http://dev.mysql.com/doc/refman/5.0/en/join.html for more details.
You need to use an inner join
select * from queuelist as ql inner join info as i on ql.clientID = i.ID
Though you might want to replace * with specific field names e.g
select ql.clientID, i.fieldname FROM....
Well, I see no difficulty in this using a JOIN.
SELECT * FROM queuelist JOIN info ON clientID = info.ID WHERE queuelist.ID = 2
"Where" would be another option.
SELECT Name, Phone FROM queuelist,info WHERE clientID = ID
Assuming you want only name and phone