PDO - select data from two tables [duplicate] - mysql

This question already has answers here:
SQL query return data from multiple tables
(6 answers)
Closed 6 years ago.
I want to take all the data from two tables with the same user_id, but I don't know what type of join to use and I want to be sure with the syntax of the query.
"users" table:
"schedule" table:

Here's a simple LEFT JOIN example:
SELECT * FROM `users` LEFT JOIN `schedule` ON `users`.`userid` = `schedule`.`user_id`
WHERE `users`.`userid` = ?
this will collect everything from both tables and then you can output it accordingly.

Related

Is there a solution to fetch data from 2 tables without left join? [duplicate]

This question already has answers here:
Data from two tables with same column names
(4 answers)
MySQL - Selecting data from multiple tables all with same structure but different data
(6 answers)
Closed 2 years ago.
I have this problem, which it's keeping me out of focus from the main project on Laravel.
I have 2 tables without any fk on them
TableA:
id(INT)
Name(VARCHAR)
EventId(INT)
PlayerChoiceId(INT)
and
TableB:
id(INT)
PlayerChoice(VARCHAR)
PlayerColor(VARCHAR)
I need to make a method on my controller to select all of the data from TableA with the PlayerChoice value, not the id from TableB, but without LEFT JOIN.
Example:
id(TableA)|Name|EventId|PlayerChoice
Something like:
SELECT a.*, b.PlayerChoice from TableA a, TableB b where a.PlayerChoiceId = b.id;
Will give you:
a.id a.Name a.EventId a.PlayerChoiceId b.PlayerChoice
is that what you're looking for?

I am getting an error in mysql update query using inner join [duplicate]

This question already has answers here:
How can I do three table JOINs in an UPDATE query?
(7 answers)
Closed 3 years ago.
I have got an error in running this query in phpMyadmin. The query is to update a table using inner join .Pls help me
My tables are :
userpswd
ID
password
Name
Col 4
Col 5
index
oncampus_present ( the colum I want to update )
oncampus
index
ID
I already have gone through most of the threads given here regarding the same and none seem to work.
UPDATE userpswd
SET oncampus_present='yes'
FROM userpswd
INNER JOIN oncampus
ON userpswd.ID=oncampus.ID
WHERE userpswd.oncampus_present=NULL
You have an error in your SQL syntax
Try This
UPDATE userpswd
INNER JOIN oncampus
ON userpswd.ID=oncampus.ID
SET oncampus_present='yes'
WHERE userpswd.oncampus_present IS NULL

Update query for three tables? [duplicate]

This question already has answers here:
MySQL, update multiple tables with one query
(7 answers)
Closed 4 years ago.
I have to pass a mysqli update query. Following are the three different tables with the situation.
I have a user_table with columns
user_id
user_email
I have a webpage_table with columns
webpage_id
first_webpage
second_webpage
I have a user_data_table with only columns
user_id
webpage_id as foreign key
Now I have to update the values of webpage_table where the user_email="johndoe#xyz.com"....
What will be my update mysqli_query() for the above situation...I tried but i'm not able to go any further of where condition.... Below is my attempt
UPDATE `webpage_table` SET `first_webpage`='xyz', `second_webpage`='xyz'
WHERE
You could use an updated based on join
with the related table
UPDATE `webpage_table`
INNER JOIN user_data_table d on u.user_id = d.user_id
and `webpage_table`.webpage_id = d.webpage_id
INNER JOIN user_table u.user_email="johndoe#xyz.com"
SET `first_webpage`='xyz',
`second_webpage`='xyz'

Method to use LEFT JOIN with columns with the same name [duplicate]

This question already has an answer here:
MySQL JOIN tables with duplicate column names
(1 answer)
Closed 6 years ago.
I am using SELECT SQL_CALC_FOUND_ROWS *, but I need to left join a table that has a column with the same name as the first one.
$sql = "
SELECT SQL_CALC_FOUND_ROWS *
FROM {$this->_db}
LEFT JOIN $this->_db2 ON $this->_db2.calc_id = $this->_db.calc_id
";
Any idea how to make this work, as now the value from the 2nd table is overwriting the fist one?
Since you are joining a table with a field with the same name as another, you cannot use SELECT *.
You need to manually list the fields that you want in the SELECT statement.

MySql update with join from another table [duplicate]

This question already has answers here:
String concatenation in MySQL
(5 answers)
Closed 6 years ago.
I added a column to my WorkSheetTransaction table and want to populate it with a name built from the Department table. Both tables are already populated with the join field DepartmentId.
The following query runs ok but no rows are updated. Why not?
update WorkSheetTransactions
inner join Departments on WorkSheetTransactions.DepartmentId = Departments.DepartmentId
set WorkSheetTransactions.DepartmentName = (Departments.GL_Account + '-' + Departments.DepartmentName)
I've tried many variations but I just can't see where I've gone wrong. BTW, the join field is an integer in both tables and all the other 2 fields are var_chars.
In mysql, you should use concat to concatenate strings:
UPDATE WorkSheetTransactions
INNER JOIN Departments
ON WorkSheetTransactions.DepartmentId = Departments.DepartmentId
SET WorkSheetTransactions.DepartmentName = concat(Departments.GL_Account, '-', Departments.DepartmentName)