MySQL join two tables which have same rows and replace specific column - mysql

What do I have now is a two tables in MySQL db, one thing happened during converting tables (forum convert), and now I've an issue with encoding. I want to fix that by joining one table to another, according to ids, and ignore first table text column while replacing it by text column from other table.
Both tables have "topic_id" and "threadid" which uses same numbers to identify thread name.
They are also have "title" and "topic_title". There is some amount of other columns, ask if you will need and I post the other ones.
So, is it possible to check while "topic_id == threadid", and replace "topic_title" with "title" using MySQL query or not?

UPDATE phpbbf_topics t1
JOIN vb_thread t2 ON t1.topic_id = t2.threadId
SET t1.topic_title = t2.title
Something like this should do it.

yes it is possible try this query
Update tbl1 A SET A.topic_title = B.title
LEFT JOIN tbl2 B ON A.topic_id = B.threadid

Related

What happen if two sql table have the same column name while trying to write a sql query from the two? [duplicate]

I have a table for users. But when a user makes any changes to their profile, I store them in a temp table until I approve them. The data then is copied over to the live table and deleted from the temp table.
What I want to achieve is that when viewing the data in the admin panel, or in the page where the user can double check before submitting, I want to write a single query that will allow me to fetch the data from both tables where the id in both equals $userid. Then I want to display them a table form, where old value appears in the left column and the new value appears in the right column.
I've found some sql solutions, but I'm not sure how to use them in php to echo the results as the columns in both have the same name.
Adding AS to a column name will allow you to alias it to a different name.
SELECT table1.name AS name1, table2.name AS name2, ...
FROM table1
INNER JOIN table2
ON ...
If you use the AS SQL keyword, you can rename a column just for that query's result.
SELECT
`member.uid`,
`member.column` AS `oldvalue`,
`edit.column` AS `newvalue`
FROM member, edit
WHERE
`member.uid` = $userId AND
`edit.uid` = $userId;
Something along those lines should work for you. Although SQL is not my strong point, so I'm pretty sure that this query would not work as is, even on a table with the correct fields and values.
Here is your required query.
Let suppose you have for example name field in two tables. Table one login and table 2 information. Now
SELECT login.name as LoginName , information.name InofName
FROM login left join information on information.user_id = login.id
Now you can use LoginName and InofName anywhere you need.
Use MySQL JOIN. And you can get all data from 2 tables in one mysql query.
SELECT * FROM `table1`
JOIN `table2` ON `table1`.`userid` = `table2`.`userid`
WHERE `table1`.`userid` = 1

How to search data from multiple tables using Mysql?

I have three tables :
bldr_prjct,bldr_prjct_attr,bldr_prjct_attr_ref;
i want to get bldr_prjct data where bldr_prjct attributes are stored in bldr_prjct_attr.
bldr_prjct_attr_ref : In this table i have defined values all attributes related to projects :
Screenshots :
bldr_prjct
bldr_prjct_attr_ref :
bldr_prjct_attr :
My query :
SELECT
`p`.`ID`,`p`.`PRJCT_NM`,`p`.`SLUG`,`p`.`STS_CD`,
`p`.`PRJCT_GEO_LT`,`p`.`PRJCT_GEO_LG`
FROM
`bldr_prjct` `p`, `bldr_prjct_attr_ref` `pr`, `bldr_prjct_attr` `pa`
WHERE
`pa`.`REF_ID` IN (SELECT `ID` FROM `bldr_prjct_attr_ref` WHERE `PRNT_ID`=3)
Firstly, don't use implicit JOIN syntax(comma separated) , use the proper syntax of join, that will help you avoid this kind of mistakes.
You query is not working because you are missing the join relations , you may have to adjust it a bit, i guessed bldr_prjct is joined to bldr_prjct_attr_ref by id=prnt_id although you have prnt_id column in this table, so change it if needed.
SELECT `p`.`ID`,`p`.`PRJCT_NM`,`p`.`SLUG`,`p`.`STS_CD`,`p`.`PRJCT_GEO_LT`,`p`.`PRJCT_GEO_LG`
FROM `bldr_prjct` `p`
INNER JOIN `bldr_prjct_attr` `pa`
ON(`p`.id = `pa`.prnt_id )
INNER JOIN `bldr_prjct_attr_ref` `pr`
ON(`pa`.ref_id = `pr`.id and `pr`.prnt_id = 3 )
I might be missing something, but your tables don't appear to have any common reference. For instance, table bldr_prjct_attr_ref does not include any field pointing to the relevant record in table bldr_prjct. If my understanding is correct, you will have to alter these tables to allow some time of cross-referencing. For instance, add to table bldr_prjct_attr_ref a field (column) bldr_prjct_ID that points to the corresponding record in table bldr_prjct.
There is one field in the third table that appears to reference a record in the first, but when you issue a SELECT of three tables and one of them returns an empty result, then the overall select returns empty.

Append records from one table to another using the common field

There are many varied posts about this matter, but I am unable to find the answer I need. I am hoping this question is unique.
I am trying to append all the data from one table to another, without creating new records. The data in the second table is really a subset of data for a portion of the existing records in the first table.
For example:
I have the table "SPK". And I want to write all of the data from SPK into the table "RCT". The common field between each record I want to match is the RegID, which is unique in both tables (i.e. there is only one SPK record per RCT record).
If I understand correctly, you mean append the columns in one table (call it SECOND) to the other (call it FIRST).
In that case, does this work ?
UPDATE
regcontactsTest
JOIN
speakersTest
ON speakersTest.RegistrationID = regcontactsTest.RegistrationID
SET regcontactsTest.presentationtitle = speakersTest.presentationtitle
EDIT: Updated the query based on Mariadb syntax
You need to use JOIN. For general Update join :
update tab1 a
join tab2 b ON a.join_colA = b.join_colB
SET a.columnToUpdate = [something]
Or in other words:
update
tab1 a
join tab2 b on ..
set a.field=...;

Getting a string from a referenced table

I am relativly new to the SQL language. I can do a basic select, but for performance increase, I'd love to know if it is possible to merge the two queries I am doing at the moment into one.
Scenario: There are two tables. Table one has a few columns, one of them is a VARCHAR(45) named 'user', and another one is a INT which is called 'gid'. In the second table, there is a primary key column called 'gid' (INT) and a column called 'permissions' which is a TEXT column and it contains values seperated by ';'.
I have a user name, and want the text in the permissions column. The current way I do it is by fetching the gid of the first table, then doing a second query with the gid to get the permissions.
I've heard there are other ways to do this, and I have searched on Google, but I'm not sure what I should do.
EDIT:
Like this:
select t2.permissions
from table1 t1, table2 t2
where t1.user = '<SPECIFIED NAME>'
and t1.gid = t2.gid;
or you could use INNER JOIN syntax:
select t2.permissions
from table1 t1
inner join table2 t2 on t1.gid = t2.gid
where t1.user = '<SPECIFIED VALUE>'
To do this you use a JOIN. A join connects two tables in a select statement.
Like this
select *
from usertable u
join permissiontable p on u.gid = p.gid
This will give you all the columns from both tables with the id column joined. You can treat the joined table just like any table (eg select a sub-set of columns in the select list, add a where clause, etc).
You can read more about joins in any intro sql book or doing a google search.

Find matches from 2 tables, change other field?

I have a database with two separate tables. One table (T1) has 400+ values in its only column, while the other (T2) has 14,000+ rows and multiple columns.
What I need to do is to compare the column in T1 to one column in T2. For every matching value, I need to update a different value in the same row in T2.
I know this is pretty easy and straight-forward, but I'm new to MySQL and trying to get this down before I go back to other things. Thanks a ton in advance!
EDIT: Here's what I've been trying to no avail..
UPDATE `apollo`.`Source`, `apollo`.`Bottom`
SET `Source`.`CaptureInterval` = '12'
WHERE `Bottom`.`URL` LIKE `Source`.`SourceID`
EDIT 2:
A little clarification:
apollo.Bottom and apollo.Source are the two tables.
apollo.Bottom is the table with one column and 400 records in that column.
I want to compare Bottom.URL to Source.SourceID. If they match, I want to update Source.CaptureInterval to 12.
You can use the following query to update. But the performance will be much better if you index URL and SourceID columns in both tables as they are being used in the WHERE clause.
UPDATE `apollo`.`Source`, `apollo`.`Bottom`
SET `Source`.`CaptureInterval` = '12'
WHERE `Bottom`.`URL` = `Source`.`SourceID`
You can join the two tables together and do a multiple table update.
Start with something like this:
UPDATE `apollo`.`Source`
INNER JOIN `apollo`.`Bottom` ON `apollo`.`Bottom`.`URL` = `apollo`.`Source`.`SourceID`
SET `apollo`.`Source`.`CaptureInterval` = '12';