I'm retrieving my records using CActiveRecord with a with() statement:
Probes::model()->with(array
(
'user',
'results',
'results.answer',
'survey',
'survey.questions'
))->findByPk($id)
I wanted to use GROUP BY on question_id field of survey.questions relation, so I changed above to:
'survey.questions'=>array('group'=>'survey.questions.question_id'),
This caused a SQL exception 1054 Unknown column. However, by analyzing attached SQL code:
`questions`.`question_id` AS `t6_c2`
I managed to find out, that I have to use t6_c2 alias (auto-generated by Yii?). So, another change to:
'survey.questions'=>array('group'=>'t6_c2'),
and the problem is solved.
But, then again, alias t6_c2 seems quite... "unstable" (auto-generated?) for me. Can I force Yii to in this part of generated SQL some other alias, provided by me? Or how certain can I be, that this part of SQL code won't change upon next (some later) generation? Or -- is there any other way to achieve, what I want to achieve?
you can assign alias to your relation
,
'survey.questions'=>array(
'alias' => 'surq'
'group'=>'surq.question_id',
),
read this and this for more info
You can set specific and unique alias for each relation table in a relations method in your model. For example,"user"=>array(self::HAS_MANY, "User", "user_id", "alias"=>"your_alias_for_this_relation")
Try this (i'm asumming 'survey.questions' is a field of the table probe)
Prove::model()->findByPk($id)->survey.questions, CHtml::listData(Prove::model()->findAll(array("order"=>"survey.questions")),
Related
I'm trying to build the following query in propel
SELECT *
FROM user
ORDER BY username = 'foo.bar' DESC, username
These options all give me errors:
UserQuery::create()
->addAscendingOrderByColumn("username = 'foo.bar'")
This is caused due to the ".". Without it's working
Errormessage:
"Criteria:(Error: Cannot fetch TableMap for undefined table: username = 'foo)"
Sadly, this is not possible in Propel 1 (nor do I know if it's possible with Propel 2). I had a similar issue:
https://groups.google.com/forum/#!topic/propel-users/sz3wA_bdF8k
And upon further investigation, I discovered that you can only pass column names and fully qualified names (table.column) into the ORDER BY methods. Switch statements do work in ->withColumn clauses though, for anyone who's trying to do something similar.
before i use alias for table i get the error:
: Integrity constraint violation: 1052 Column 'id' in field list is ambiguous
Then i used aliases and i get this error:
unknown index a
I am trying to get a list of category name ( dependant to a translation) and the associated category id which is unique. Since i need to put them in a select, i see that i should use the lists.
$categorie= DB::table('cat as a')
->join('campo_cat as c','c.id_cat','=','a.id')
->join('campo as d','d.id','=','c.id_campo')
->join('cat_nome as nome','nome.id_cat','=','a.id')
->join('lingua','nome.id_lingua','=','lingua.id')
->where('lingua.lingua','=','it-IT')
->groupby('nome.nome')
->lists('nome.nome','a.id');
The best way to debug your query is to look at the raw query Laravel generates and trying to run this raw query in your favorite SQL tool (Navicat, MySQL cli tool...), so you can dump it to log using:
DB::listen(function($sql, $bindings, $time) {
Log::info($sql);
Log::info($bindings);
});
Doing that with yours I could see at least one problem:
->where('lingua.lingua','=','it-IT')
Must be changed to
->where('lingua.lingua','=',"'it-IT'")
As #jmail said, you didn't really describe the problem very well, just what you ended up doing to get around (part of) it. However, if I read your question right you're saying that originally you did it without all the aliases you got the 'ambiguous' error.
So let me explain that first: this would happen, because there are many parts of that query that use id rather than a qualified table`.`id.
if you think about it, without aliases you query looks a bit like this: SELECT * FROM `cat` JOIN `campo_cat` ON `id_cat` = `id` JOIN `campo` ON `id` = `id_campo`; and suddenly, MySQL doesn't know to which table all these id columns refer. So to get around that all you need to do is namespace your fields (i.e. use ... JOIN `campo` ON `campo`.`id` = `campo_cat`.`id_campo`...). In your case you've gone one step further and aliased your tables. This certianly makes the query a little simpler, though you don't need to actually do it.
So on to your next issue - this will be a Laravel error. And presumably happening because your key column from lists($valueColumn, $keyColumn) isn't found in the results. This is because you're referring to the cat.id column (okay in your aliased case a.id) in part of the code that's no longer in MySQL - the lists() method is actually run in PHP after Laravel gets the results from the database. As such, there's no such column called a.id. It's likely it'll be called id, but because you don't request it specifically, you may find that the ambiguous issue is back. My suggestion would be to select it specifically and alias the column. Try something like the below:
$categories = DB::table('cat as a')
->join('campo_cat as c','c.id_cat','=','a.id')
->join('campo as d','d.id','=','c.id_campo')
->join('cat_nome as nome','nome.id_cat','=','a.id')
->join('lingua','nome.id_lingua','=','lingua.id')
->where('lingua.lingua','=','it-IT')
->groupby('nome.nome')
->select('nome.nome as nome_nome','a.id as a_id') // here we alias `.id as a_id
->lists('nome_nome','a_id'); // here we refer to the actual columns
It may not work perfectly (I don't use ->select() so don't know whether you pass an array or multiple parameters, also you may need DB::raw() wrapping each one in order to do the aliasing) but hopefully you get my meaning and can get it working.
I am trying to select certain values from a view that I created. The statement is below:
SELECT * FROM dashboard.team
WHERE ac2012.acx_users.id = 1;
As you can see, there are 2 databases being referenced here:
dashboard database, team table
ac2012 database, acx_users.id table
ac2012.acx_users.id is the regular expression in the original Create View statement, I'm using that since of course I can't use an ALIAS in a Where clause... however, this is showing an error:
Error Code 1054: Unknown column 'ac2012.acx_users.id' in 'where clause'
I'm not sure how to get this to work, because I need to reference the other database in this case, but it's not recognizing the database. Any tips would be appreciated.
Since you're selecting from a view, the underlying databases aren't visible anymore. You only see what the view presents, as part of the database which the view lives in, so try WHERE acx_users.id = 1, or whatever you've aliased that field to in the view definition.
SELECT * FROM dashboard.team
LEFT OUTER JOIN ac2012 ON ac2012.CommonColumnName=dashboard.CommonColumnName
WHERE ac2012.acx_users.id = 1;
======================
Please replace by original column name ...
Is there any easier way of writing sql to replace all columns names with "schedule_" to make it easier to work with in PHP.
SELECT * FROM schedules
Array
(
[schedule_id] => 9
[schedule_datetime_from] => 2011-12-22 18:28:00
[schedule_datetime_until] => 2011-12-22 22:28:00
[schedule_capacity] => 89
[schedule_amount] => 9.99
[content_id] => 77
)
At the moment I end up doing:
$stmnt1 = "SELECT s.schedule_id as id, s.schedule_datetime_from as datetime_from, s.schedule_datetime_until as datetime_until, etc FROM schedules s";
There is no other way to do this through SQL -- and really, why would you want to? It's not like you are typing in the query by hand each time.
What you could do is write PHP code that changes the array keys after each row has been read, but that's orders of magnitude worse as a solution. Just go with what you already have.
I agree with Jon, but if you REALLY have to get this done I would recommend using the MySQL command line interface. However, you must also consider indices, UNIQUE constraints, and Foreign Keys. And, if you have any code which is expecting the non-prefixed versions of the columns you will end up breaking it, so be sure your code is updated as well.
Read up on ALTER TABLE CHANGE via MySQL docs, the line you are looking for is: "You can rename a column using a CHANGE old_col_name new_col_name column_definition clause"
I keep getting MySQL error #1054, when trying to perform this update query:
UPDATE MASTER_USER_PROFILE, TRAN_USER_BRANCH
SET MASTER_USER_PROFILE.fellow=`y`
WHERE MASTER_USER_PROFILE.USER_ID = TRAN_USER_BRANCH.USER_ID
AND TRAN_USER_BRANCH.BRANCH_ID = 17
It's probably some syntax error, but I've tried using an inner join instead and other alterations, but I keep getting the same message:
Unknown column 'y' in 'field list'
Try using different quotes for "y" as the identifier quote character is the backtick (`). Otherwise MySQL "thinks" that you point to a column named "y".
See also MySQL 8 Documentation
Please use double-/single quotes for values, strings, etc.
Use backticks for column-names only.
Enclose any string to be passed to the MySQL server inside single quotes, e.g.:
$name = "my name"
$query = " INSERT INTO mytable VALUES ( 1 , '$name') "
Note that although the query is enclosed between double quotes, you must enclose any string in single quotes.
You might check your choice of quotes (use double-/ single quotes for values, strings, etc and backticks for column-names).
Since you only want to update the table master_user_profile I'd recommend a nested query:
UPDATE
master_user_profile
SET
master_user_profile.fellow = 'y'
WHERE
master_user_profile.user_id IN (
SELECT tran_user_branch.user_id
FROM tran_user_branch WHERE tran_user_branch.branch_id = 17);
Just sharing my experience on this. I was having this same issue. The insert or update statement is correct. And I also checked the encoding. The column does exist.
Then! I found out that I was referencing the column in my Trigger.
You should also check your trigger see if any script is referencing the column you are having the problem with.
In my case, it was caused by an unseen trailing space at the end of the column name. Just check if you really use "y" or "y " instead.
While working on a .Net app build with EF code first, I got this error message when trying to apply my migration where I had a Sql("UPDATE tableName SET columnName = value"); statement.
Turns out I misspelled the columnName.
If it is hibernate and JPA. check your referred table name and columns might be a mismatch
Just sharing my experience on this. I was having this same issue. My query was like:
select table1.column2 from table1
However, table1 did not have column2 column.
In my case, the Hibernate was looking for columns in a snake case, like create_date, while the columns in the DB were in the camel case, e.g., createDate.
Adding
spring:
jpa:
hibernate:
naming: # must tell spring/jpa/hibernate to use the column names as specified, not snake case
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
to the application.ymlhelped fix the problem.
In my case, I used a custom table alias for the FROM table, but I used the default table alias (MyTable) in the field list instead of the custom table alias (t1). For example, I needed to change this...
mysql> SELECT MyTable.`id` FROM `MyTable` t1;
...to this...
mysql> SELECT t1.`id` FROM `MyTable` t1;
In my case I had misspelled the column name in the table's trigger. Took me a while to connect the error message with the cause of it.
I too got the same error, problem in my case is I included the column name in GROUP BY clause and it caused this error. So removed the column from GROUP BY clause and it worked!!!
I got this error when using GroupBy via LINQ on a MySQL database. The problem was that the anonymous object property that was being used by GroupBy did not match the database column name. Fixed by renaming anonymous property name to match the column name.
.Select(f => new
{
ThisPropertyNameNeedsToMatchYourColumnName = f.SomeName
})
.GroupBy(t => t.ThisPropertyNameNeedsToMatchYourColumnName);
A query like this will also cause the error:
SELECT table1.id FROM table2
Where the table is specified in column select and not included in the from clause.