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.
Related
This question already has answers here:
Filter Table Before Applying Left Join
(4 answers)
Closed 2 years ago.
I want to join two tables based on the below scenarios:
Need to display all the requirements table values
Join the second table(prod_spec) based on the requirement_id and inquiry_id.
Table 1(requirements):
Table 2(prod_spec):
Using below query I'm getting the result below:
SELECT requirements.id,requirements.requirement, prod_spec.evidence,prod_spec.status,prod_spec.no_specify,prod_spec.inquiry_id FROM requirements LEFT JOIN product_spec ON prod_spec.requirement_id=requirement.id
The problem is not getting any result when I put the where condition eg: where inquiry_id='67' to the sql query. Please let me know how to display the rows based on inquiry_id.
FYI, requirement_id will only there once in prod_spec table based on inquiry_id.
The problem is not getting any result when I put the where condition eg: where inquiry_id = '67' to the sql query
The condition on the LEFT JOINed table needs to go to the ON clause of the JOIN:
SELECT r.id,r.requirement, ps.evidence,ps.status, ps.no_specify, ps.inquiry_id
FROM requirements r
LEFT JOIN prod_spec
ON ps.requirement_id = requirement.id
AND ps.inquiry_id = 67 --> here
Rationale: if you put that condition in the WHERE clause, then it becomes mandatory; as a consequence, rows from requirements for which there is no match in prod_spec are evicted from the resultset.
Side notes:
table aliases make the query easier to write and read
it seems like inquiry_id is a number, so it should be compared as such; don't put single quotes around literal 67
This question already has answers here:
What is the difference between "INNER JOIN" and "OUTER JOIN"?
(28 answers)
Closed 5 years ago.
Order Table
Product Table
I need to select all from these two tables. Is it possible?
With the following query you can select all data from one table:
SELECT * FROM Order;
If you want to select all data from two tables you can use a join in the select query to connect the data of the two tables:
SELECT * FROM Order
JOIN Product ON Order.p_id = Product.p_id;
There are different kinds of joins available. Based on the one you use can change the amount of data you will receive. The different kinds of joins are:
(Inner) join: Returns records that have matching values in both tables
Left (Outer) join: Return all records from the left table, and the matched records from the right table
Right (outer) join: Return all records from the right table, and the matched records from the left table
Full (outer) join: Return all records when there is a match in either left or right table
The information about the different kinds of joins comes from the following website:https://www.w3schools.com/sql/sql_join.asp
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.
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)
This question already has answers here:
mysql extra columns with same name from two tables
(3 answers)
Closed 8 years ago.
I'm not even sure how exactly to ask this question so bear with me.
I have two tables, meets and locations, they are linked by the meet's 'loc' and the location's 'id'
I'm using this query:
$query = "SELECT * FROM meets LEFT JOIN locations ON locations.id=meets.loc ORDER BY date DESC";
$meets = mysqli_query($con, $query);
though it joins the table successfully I lose the meet's 'id' because it's being overwritten by the location's 'id'. So I end up with two identical entries 'id' and 'loc'.
Is there any way to avoid this because I need to call on the meet id?
do not select *, select the columns you need and rename them using the as key word like so
SELECT locations.id as loc_id, meets.id as meets_id, ... FROM meets LEFT JOIN locations ON locations.id=meets.loc ORDER BY date DESC
replacing ... with other columns you would like to select and possible renames of them.
You could alias the locations.id with a query like:
SELECT meets.*, locations.id AS loc_id FROM meets LEFT JOIN locations ON locations.id=meets.loc ORDER BY date DESC
You could add any other columns from locations that you might also need. You could further only select explicit columns from meet rather than meet.*
you could try to use SELECT meets.*, locations.id as locationId, locations.<row> as location<Row>, ...
Not sure if the syntax is exactly the same, but in SQL Server the syntax would be:
SELECT m.*, l.id as LocationId
FROM meets m
LEFT JOIN locations l
ON l.id=m.loc
ORDER BY date DESC