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)
Related
I have scenario where I have to find the sum of multiple columns from table2 and I have the value in where clause from another table1.
I wrote mySql query for the same as follow. I need to write it in the jooq.
select (sum(t2.column1)+sum(t2.column2)+sum(t2.column3)) as total_amount
from db.table1 t1, db.table2 t2
where t1.column1 = ‘value1’ and t1.column2 = t2.column4;
As a general rule of thumb, all functions are available from org.jooq.impl.DSL by the same name, and all operators are available from org.jooq.Field by a name that reflects the way the operator is pronounced. In your case, use:
DSL.sum(Field)
Field.plus(Field)
Specifically, assuming this static import:
import static org.jooq.impl.DSL.*;
Write:
Table1 t1 = TABLE1.as("t1");
Table2 t2 = TABLE2.as("t2");
ctx.select(sum(t2.COLUMN1).plus(sum(t2.COLUMN2)).plus(t2.COLUMN3)).as("total_amount"))
.from(t1, t2)
.where(t1.COLUMN1.eq("value1"))
.and(t1.COLUMN2.eq(t2.COLUMN4))
.fetch();
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
Is it possible to get all the tables and views used inside a user defined function? Is there any defined procedure to get that by passing the udf name?
create function sample_function
return table
as
return
select *
from table1 t1
join table2 t2 on t2.id = t1.id
I need to get table1 and table2 when i pass sample_function to any procedure.
Any Ideas would help.
Hope this helps.
SELECT DISTINCT referenced_entity_name FROM sys.dm_sql_referenced_entities('dbo.fn_sample','OBJECT')
EDIT:
Use the below query, to get only the referenced tables
SELECT DISTINCT referenced_entity_name
FROM sys.dm_sql_referenced_entities('dbo.fn_sample','OBJECT') ro
INNER JOIN sysobjects so ON (ro.referenced_id = so.id)
WHERE so.xtype = 'U'
I have two tables with "Field Name" columns. Some Table B field names are the same as Table A field names. If that is the case, I want to exclude those from the combobox so I don't have a double (I only want the Table A field name in that case). I also need the ID's (unique to each table) in the combobox.
I can't seem to come up with the right SQL logic. Right now, I'm trying the following
SELECT [fldID], [fldName] FROM OISInfo UNION
(SELECT [ID], [Field Name] FROM FldDef
LEFT JOIN OISInfo ON [Field Name] = [fldName] WHERE [fldName] IS NULL)
but Access keeps telling me that the join expression is not supported (in the bracketed part). The table names are definitely correct.
What am I doing wrong?
Tested.Worked perfectly. Table5 is your table A or maybe OISInfo. Table 6 is your table B (FldDef)
SELECT Table5.ID, Table5.Field1
FROM Table5
UNION
SELECT Table6.ID, Table6.Field1
FROM Table6 LEFT JOIN Table5 ON Table6.[Field1] = Table5.[Field1]
WHERE (((Table5.Field1) Is Null));
Union takes cares of doubles, this is all you have to do
SELECT [fldID], [fldName] FROM OISInfo
UNION
SELECT [ID], [Field Name] FROM FldDef
Warning: vagueness & unclear questioning will abound because I know squat about databases.
I just discovered that I need to use views as surrogates for a cronned update statement. I can somewhat get the view to work, but I'm having trouble with rows.
This post helped me to bang out the update I need, but now that I know that views can run that update whenever it's needed rather than on a cron schedule, how can I set the view's column value based upon the view's row id or equivalent?
I've got the select I need:
SELECT SUM( table2.column1/ (
SELECT table2constant
FROM table3
)
FROM table2
WHERE table2table1id = table1id
table1id is the AI id column for table1. table2table1id is PKd to table1id. I'd like the view to have a column PKd to table1id like with table2, and the view needs to have every distinct table1id represented.
I'm sure the jargon's way off, but hopefully you can see what I need.
Will provide as many edits as necessary for clarity.
Many thanks in advance!
EDIT1
Should I create a trigger that creates the view upon insert to table1? Just found about materialization which is what I need/want?
Clarity
I need a summed value for each table1.table1id
Progress
With this code, I'm getting the first id from table1 and only the total sum. I need a sum for each table1.id.
CREATE VIEW db1.sums as
SELECT SUM( table2.column1/ (
SELECT table2constant
FROM table3
) as theSum, table1id
FROM table1, table2
WHERE table2.table2table1id = table1.table1id
To be clear I'm still not sure what you're trying to accomplish here but if what you posted works, try
SELECT table1.table1id,
SUM( table2.collumn1 ) / (SELECT table2constant FROM table3 ) as theSum
FROM table1, table2
WHERE table2.table2table1id = table1.table1id GROUP BY table1.table1id
you can replace (SELECT table2constant FROM table3 ) with your constant if it has no reason to otherwise be in the database (if it's not updated)
Its actually very simple. Here is an example of how you can do it.
SELECT SUM( table1.column / table2.column ), table1.*, table2.*
FROM table1, table2
WHERE table1.id = table2.column_id