I am not that good at SQL but i do as much as i can for the little knowledge i have..
I have made a single a flat SQL string with the help from a friend that gathers data from a table using a relative table from an initial data from the first table, the SQL was made like this:
SELECT id, username, auth_assignment.created_at
FROM `user` JOIN `auth_assignment`
ON (user.id = auth_assignment.user_id)
JOIN `auth_item`
ON (auth_item.name = auth_assignment.item_name)
WHERE auth_item.name = 'Admin'
the initial data to look is Admin so everything works in that side, but i tried to simulate this SQL using Yii2 functions.. so far i have made this code
$query = new Query;
$command = $query->select('id, username')
->innerJoin('auth_assignment', ['user.id'=>'auth_assignment.user_id'])
->innerJoin('auth_item', ['auth_item.name'=>'auth_assignment.item_name'])
->where('auth_item.name = :name', [':name'=>$name])->createCommand();
var_dump($command->query());
this returns an SQLSTATE error:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN `auth_assignment` ON `user`.`id`='auth_assignment.user_id' INNER JOIN' at line 1
The SQL being executed was: SELECT `id`, `username` INNER JOIN `auth_assignment` ON `user`.`id`='auth_assignment.user_id' INNER JOIN `auth_item` ON `auth_item`.`name`='auth_assignment.item_name' WHERE auth_item.name = 'Admin'
i checked the method $command->sql; to know how the SQL was being generated.. but i really don't know how to fix it due to my lack of my knowledge to SQL and lack of understanding to yii2 api documentation
SQL is generated like this:
SELECT `id`, `username` INNER JOIN `auth_assignment` ON `user`.`id`=:qp1 INNER JOIN `auth_item` ON `auth_item`.`name`=:qp2 WHERE auth_item.name = :name
i appreciate any help
Try This Query
$query = (new yii\db\Query())
->select('id, username, auth_assignment.created_at')
->from('user')
->innerJoin('auth_assignment','user.id=auth_assignment.user_id')
->innerJoin('auth_item','auth_item.name = auth_assignment.item_name')
->where([
'auth_item.name' => 'Admin'
])->all();
Related
I need some help with the query below - I am trying to pull information regarding price and multiply with the quantity & insert the sum into the table. So far I have,
update passenger_baggage
SET passenger_baggage.total_baggage_cost=passenger_baggage.passenger_baggage_quantity*baggage_type.baggage_type_cost
FROM passenger_baggage INNER JOIN baggage_type
ON passenger_baggage.passenger_baggage_id = baggage_type.baggage_type_id
WHERE passenger_id = "3";
and getting this error
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use
near 'FROM passenger_baggage INNER JOIN baggage_type ON
passenger_baggage.passenge...' at line 3
Expecting the query to multiply the two values & insert the total.
There is no FROM clause in an UPDATE query.
Try with this modified query:
update passenger_baggage
INNER JOIN baggage_type
ON passenger_baggage.passenger_baggage_id = baggage_type.baggage_type_id
SET passenger_baggage.total_baggage_cost = passenger_baggage.passenger_baggage_quantity * baggage_type.baggage_type_cost
WHERE passenger_id = "3";
Try this:
UPDATE passenger_baggage, baggage_type
SET passenger_baggage.total_baggage_cost = passenger_baggage.passenger_baggage_quantity * baggage_type.baggage_type_cost
WHERE passenger_baggage.passenger_baggage_id = baggage_type.baggage_type_id AND passenger_id = "3";
Saw an example from the MySQL doc (https://dev.mysql.com/doc/refman/8.0/en/update.html)
I got an error while updating the table with the join query. The error message is:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from employee details join employeedept on employee details.emp_dept=employeedept.' at line 3
Can anyone help me to resolve this error?
update employeeDetails
set employeeDetails.emp_age = employeeDetails.emp_age+20, employeeDetails.emp_salary = employeeDetails.emp_salary-500
from employeeDetails
join employeedept on employeeDetails.emp_dept = employeedept.dept_name
where employeedept.dept_name = 'CSE';
select * from employeeDetails;
List item
Have you tried using MySQL's UPDATE JOIN syntax yet? e.g.:
update employeeDetails
join employeeDept on employeeDetails.emp_dept = employeeDept.dept_name
set
employeeDetails.emp_age = employeeDetails.emp_age+20,
employeeDetails.emp_salary = employeeDetails.emp_salary-500
where employeeDept.dept_name = 'CSE';
select * from employeeDetails;
I am trying to do an LEFT Join in Yii v.1 Mysql .
There are alreday 4 tables joined and I want to join this table also.
$cmd = Yii::app()->db->createCommand();
$selectString .= "table2.dir_pic, table2.pic1, table2.pic2 , table2.pic3 ";
$cmd->select($selectString);
$cmd->from('table1');
$cmd->leftjoin('table2', 'table1.id=table2.user_entry_id');
$entries = $cmd->queryAll();
$this->render('index', compact('entries', 'model'));
It is giving an CDbException
CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '.`dir_pic`, `table2`.`pic1`, `table2`.`pic2`, ' at line 1.
I tried this query in mysql(phpmyadmin)
SELECT table1.id,table2.dir_pic,table2.pic1,table2.pic2, table2.pic3 FROM
table1 LEFT JOIN table2 ON table1.id = table2.user_entry_id
and It shows proper output.BUt not when I try in Yii .
I found the solution. Actually I missed an comma (,) after the end of previous $selectString query.
Not done any programming for a few years so am somewhat rusty again.
I have two tables users table and users_profiles table. I want to update users_profiles table on the condition that user_uid matches on both tables (users.user_uid and users_profiles.user_uid) and where users.user_login = 'johndoe' and users.user_uid = '11'.
It will be executed in a php script, so johndoe would actually be users username (whatever is stored in session and same for users_uid. For simplicitity i added dummy data.
I run the query in phpmyadmin and get syntax error near INNER JOIN. I just cannot figure out what i'm doing wrong (probably wrote it entirely wrong) and have spent few hours trying to work it out without success.
Heres my sql query.
UPDATE
users_profiles
SET
users_profiles.user_fname = 'John',
users_profiles.user_lname = 'Doe',
users_profiles.user_gender = 'male'
INNER JOIN
users
ON
users.user_uid = users_profiles.user_uid
WHERE
users.user_login = 'johndoe'
AND
users.user_uid = '11'
error i get when running sql query via phpmyadmin.
#1064 - You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'ON users.user_uid, .user_uid FROM users_profiles WHERE
users.user_login ' at line 7
Thanks.
Try this:
UPDATE
users_profiles
INNER JOIN
users
ON
users.user_uid = users_profiles.user_uid
SET
users_profiles.user_fname = 'John',
users_profiles.user_lname = 'Doe',
users_profiles.user_gender = 'male'
WHERE
users.user_login = 'johndoe'
AND
users.user_uid = '11'
Try to exchange SET and INNER JOIN as shown, for example, in this SO answer:
UPDATE
users_profiles p
INNER JOIN
users u
ON
u.user_uid = p.user_uid
SET
p.user_fname = 'John',
p.user_lname = 'Doe',
p.user_gender = 'male'
WHERE
u.user_login = 'johndoe'
AND
u.user_uid = 11
I have a problem with my SQL. I would like to use a select to make an update, but so far I haven't been able to acomplish it. I would really apreciate if anyone could please help me. I'm quite new at MySQL. THe query in question is this:
UPDATE
pac
SET
pac.pac_spc_id = dtn_atual.spc_id
FROM
pac_paciente pac
INNER JOIN ( SELECT
dtn.dtn_pac_id,
spc_id
FROM `pac_destino` dtn
INNER JOIN ( select
des.dtn_pac_id,
MAX(CONCAT(des.dtn_data, des.dtn_seq)) AS datamax
FROM pac_destino des
GROUP BY des.`dtn_pac_id`
) AS d1 ON d1.dtn_pac_id = dtn.dtn_pac_id
AND d1.datamax = CONCAT(dtn.dtn_data, dtn.dtn_seq)
LEFT JOIN pac_classdestino c1 ON c1.cdo_id = dtn.dtn_cdo_id
LEFT JOIN pac_statuspaciente spc ON spc.spc_id = c1.cdo_spc_id
) AS dtn_atual on dtn_atual.dtn_pac_id = pac.`pac_id`
The error generated is
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax
to use near 'FROM pac_paciente pac INNER JOIN ( SELECT
dtn.dtn_pac_id, spc_id
' at line 5