Ambiguous field names / add table name with FETCH_OBJ - mysql

I've got a table T1 with 2 foreign keys targeting the same table T2.
So, my SQL statement is like that :
SELECT *
FROM T1
INNER JOIN T2 AAA ON AAA.ID = T1.ID1
INNER JOIN T2 BBB ON BBB.ID = T1.ID2
(this sql works)
Then, I use $info = $exec->fetch(PDO::FETCH_ASSOC);
I'd like, at end, use the $info->AAA.name and $info->BBB.name,
but I cannot find how to set the table name into the reading. And I'd
like to avoid to change my SQL by adding dozen of ... AS ...
Any idea?

UPDATE1
I have searched through PDO documentation and found directive PDO::ATTR_FETCH_TABLE_NAMES. Docs say that it makes driver to return table names before column names. Table names will be separated from column names by '.'(dot).You could try to use this directive to solve your problem, but perhaps it will not work without aliases..
Please try this on: $PDO->setAttribute(PDO::ATTR_FETCH_TABLE_NAMES, true);
UPDATE2
I have found following question and answer from the asking person, who have used this directive to solve his problem, similar to yourth.
SELECT result with table names for columns
This question dublicates several other questions asked earlier:
PDO: fetchAll() with duplicate column name on JOIN
PDO Auto-aliasing when fetching?
So, the first answer was: "NO, you cannot do this without setting aliases for each column."
Workaround is to use PDO::FETCH_NUM, which will return you a numbered array of fetched columns. But I would not recomend it, because if you will change your tables' structure, your PHP script may become broken.
Anyway, it is still a good practise to select certain columns and set aliases instead of all *. Like this:
SELECT T1.ID1 as ID1, T1.ID2 as ID2, T1.name as T1_name, AAA.name as AAA_name, BBB.ID as BBB_name
FROM
T1
INNER JOIN
T2 AAA
ON AAA.ID = T1.ID1
INNER JOIN
T2 BBB
ON BBB.ID = T1.ID2

Related

Update a MySQL table with index from another table based on value set in both tables

I am not sure how to ask this question - it might be answered somewhere, but it's kind of messy ;)
There are two tables that sort flags (labels) for objects somewhere else.
Table 1: the flags in booka_flags
columns of interest are id(INT) and title(VARCHAR)
Table 2: the flag items in booka_flags_items
columns of interest are id(INT) and flag(VARCHAR)
I want to change booka_flags_items.flag from VARCHAR (which is available from booka_flags.title) to ID (which is available from booka_flags.id)
My "idea" of writing this query (after some research) was the following:
UPDATE
booka_flags_items
SET
booka_flags_items.flag = booka_flags.id
FROM
booka_flags_items
INNER JOIN
authors
ON
booka_flags_items.flag = booka_flags.title
but it does not work. Also not working is this query:
UPDATE
booka_flags_items
SET
booka_flags_items.flag = (
SELECT booka_flags.id
FROM booka_flags
INNER JOIN booka_flags_items
ON booka_flags_items.flag = booka_flags.title
)
What would be the right way to solve a query like that?
I'm guessing you would like to update id column of booka_flags with id column of booka_flags where title matches flags. Your updated query looks incorrect as per mysql
Try:
UPDATE booka_flags_items t1 INNER JOIN booka_flags t2 On t1.flag = t2.title
SET t1.id = t2.id

Append text from one table to another table

Got a small question in which I can't wrap my head around..
I have 2 tables, 1 table where there is a Youtube Link i need to append to another column in table 2. But not every record has a Youtube link, which means I need to check if in both tables the names are the same (WHERE name1 = name2?) But how can I append text to already existing text in table 2? Something like this?
UPDATE table2 SET text2 = (text2 + '/n' + table1.text1) WHERE name1 = name2?
If someone could help me, would be awesome!
EDIT:
So I fumbled a bit with the queries:
SELECT
'Kleding'.'Naam',
'jos_virtuemart_products_nl_nl'.'product_name',
CONCAT_WS('/n', 'jos_virtuemart_products_nl_nl'.'product_desc', 'Kleding'.'Youtube_link')
FROM
'jos_virtuemart_products_nl_nl' as 't2'
INNER JOIN 'Kleding' as 't1'
ON 't2'.'product_name' = 't1'.'Naam';
But this query is incorrect for some strange reason. I cannot find why it is incorrect.
You can use INNER JOIN in UPDATE:
UPDATE
table2 as t2 INNER JOIN table1 as t1 ON t2.name = t1.name
SET
t2.text2 = CONCAT_WS('/n', t2.text2, t1.text1);
P.S.: Maybe by '/n' you mean '\n' ?
P.P.S: Before any major data change operation I suggest you to backup your database.
UPDv1:
Test your end resultset with SELECT:
SELECT
`t1`.`name` as `t1-name`,
`t2`.`name` as `t2-name`,
CONCAT_WS('/n', `t2`.`text2`, `t1`.`text1`) as `changes`
-- ^ here you will see what comes up
FROM
`table2` as `t2` INNER JOIN `table1` as `t1` ON `t2`.`name` = `t1`.`name`
-- WHERE possibly more conditions to meet your requirements
If you manage te get proper resultset with additional WHERE conditions, then move them to the UPDATE statement.

add multiple (decimals) values from two tables to update a third

This SHOULD be trivial but I am going round in circles, perhaps someone can help.
I have two tables (T1, T2) from which I wish to extract a number of values in each row and update the contents of a third table (T3) iff (if and only if) two UQ, NN fields in T1, T2 match, in which case I want to add some of the values in the corresponding rows from T1, T2 together and put them in T3 and copy some other values over to T3.
The fields to be summed are all declared DECIMAL.
Simplified and in pseudocode (to avoid making too many assumptions):
SELECT T1.a,T1.b,T1.c from T1
SELECT T2.d, T2.e from T2
UPDATE T3.col1=a, T3.col2=b, T3.col3 = (value of(T2.c) + value of(T2.e)) iff T1.a = T2.d
A variety of attempts have failed to work.
I am running MySQL Workbench 5.2.37 on Ubuntu 12.10
Example from comment below:
UPDATE Test_join as T3
SELECT GZHident, Magnitude_1 from GZHTableExtended3 as T1
SELECT AHZid, DM from AHZDMConversionTable as T2 JOIN T2,T1
ON T1.GZHident = T2.AHZid
SET T3.AHZid = T1.GZHident
SET T3.DM = T2.DM
SET T3.Abs_Magnitude_1 = T1.Magnitude_1 + T2.DM;
MySQL supports multi-table UPDATE using JOIN syntax, just like SELECT. This isn't standard ANSI SQL, but it's very useful for these types of situations.
Example:
UPDATE T1
JOIN T2 ON T1.a = T2.d
JOIN T3 ON ...???...
SET T3.col1=T1.a,
T3.col2=T1.b,
T3.col3 = T2.c + T2.e;
You don't have enough information in your question for me to guess how to join to T3.
Thanks for posting an example UPDATE query. But it's still not clear how you want to update T3. The sample query you give is not valid SQL code at all. I suggest you read a tutorial on programming in SQL.
You seem be confused between UPDATE and INSERT? Keep in mind:
INSERT adds new rows.
UPDATE changes values in existing rows.
I'm guessing from your wording, but perhaps you want to add new rows. You can do that in this way:
INSERT INTO Test_join (AHZid, DM, Abs_Magnitude)
SELECT T1.GZHident, T2.DM, T1.Magnitude_1 + T2.DM
FROM GZHTableExtended3 as T1
JOIN AHZDMConversionTable as T2 JOIN T2
ON T1.GZHident = T2.AHZid;

Issue with the TOP 1 query

Is it possible to achieve next thing without using views, but just one single query? I have two tables:
TableA->TanbleB (1-many) ON TableA.Id = TableB.TableAId
I need to update one field in Table A (TableA.Field1) for records in TableA that satisfy condition on one field in tableA (WHERE TableA.Field2=SomeValue)
.
TableA.Field1 will be updated from TableB with value that is last inserted (last inserted value in related records to TableA).
I will put an example:
UPDATE TableA a SET Field1 = (SELECT TOP 1 b.Feild1 * b.Field2 FROM TableB b WHERE b.TableAId = a.id) WHERE field2 = 1
I know Above example doesn't work, but I have many ways tried using INNER JOIN and failed. I had an idea to use something like this:
UPDATE TableA INNDER JOIN ( SELECT ... FROM TABLE B) ON TABLEA.Id= TableB.TableAId SET ....
But the 2ns query should return 1 record for each DISTINCT TableAId, but only the last inserted.
I hope I am making some sense here.
Thanks in advance.
Here is some SQL that will do what you want
UPDATE T1 INNER JOIN T2 ON T1.ID = T2.T1ID SET T1.F2 = [T2].[F2]*[T2].[F3] WHERE (((T1.F1)="ABC") AND ((T2.ID)=DMax("[ID]","[T2]","[T1ID]=" & [T1].[ID])));
This predicated on T1.ID being the primary key for T1 and T2.T1ID being a index field in T2
One of the flaws in Access is that you can't run an "UPDATE" query based on a "SELECT" query, it will usually give the error:
Operation must use an updateable query
The only way around is as you say to create a view of the "SELECT" query and then inner join this on your table, Access is then working with a static recordset and can handle the "UPDATE" query ok
Alternatively you could write a VBA procedure to step through line by line with the Recordset.
Best of luck : )
UPDATE:
SELECT b.TableAId, b.Feild1 * b.Field2 INTO tblView FROM TableB As b WHERE b.field2 = 1

Ambiguous Column SQL error when using a concatenated field

I am buidling an app in CakePHP. I have 2 models:
- Project
- User
The Project model has various belongsTo relations to the user model, one for the creator, one for the last editor and one for the manager. This works fine.
Then I add a virtual field to the User model, called 'name', which is CONCAT(first_name, ' ', last_name). It combines the first name and last name into a general name field, which is used througout the app.
After this, I get SQL errors saying that the first_name column is ambiguous. This is because in the query, the alias for Creator, Manager, etc is not used in the CONCAT field.
Any ideas on how to avoid this?
Showing the exact queries should help resolve this problem. But if you are joining 2 tables, and they both have a column with the same name. you have to reference the column with TableName.ColumnName, like this.
Select Table1.Column1 AS someColumn, Table2.Column1 AS SomeOtherColumn
FROM Table1
INNER JOIN Table2
ON Table1.ID = Table2.Table1ID
WHERE Table1.ID = 3
You can shorten this up by giving your tables aliases, As follows.
Select T1.Column1 AS someColumn, T2.Column1 AS SomeOtherColumn
FROM Table1 AS T1
INNER JOIN Table2 AS T2
ON T1.ID = T2.Table1ID
WHERE T1.ID = 3
I found the solution: http://book.cakephp.org/view/1632/Virtual-fields-and-model-aliases
Try specifying what table the first_name column is from. Something like this:
CONCAT(table1.first_name,'',table1.last_name)