How can you perform a WHERE query in MySQL using a value found in a specific cell
for example if you have a table full of people and it has the columns first name, middle name, surname perform a SELECT WHERE which finds a single person and then use that persons middle name to find all people with that middle name as their last name.
CodePudding user response:
Do a self join, something like:
select *
from persons p1
left join persons p2 on p2.lastname = p1.middlename
where p1.firstname = 'megaman'
CodePudding user response:
You can use sub queries to find the person of the middle name but inside the WHERE clase of another query this is an example of code
SELECT * FROM CUSTOMERS WHERE ID IN (SELECT ID FROM CUSTOMERS WHERE SALARY > 4500) ;
A Subquery or Inner query or a Nested query is a query within another SQL query and embedded within the WHERE clause.
A subquery is used to return data that will be used in the main query as a condition to further restrict the data to be retrieved.
Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
There are a few rules that subqueries must follow −
Subqueries must be enclosed within parentheses.
A subquery can have only one column in the SELECT clause, unless multiple > >columns are in the main query for the subquery to compare its selected > >columns.
An ORDER BY command cannot be used in a subquery, although the main query >can use an ORDER BY. The GROUP BY command can be used to perform the same >function as the ORDER BY in a subquery.
Subqueries that return more than one row can only be used with multiple >value operators such as the IN operator.
The SELECT list cannot include any references to values that evaluate to a >BLOB, ARRAY, CLOB, or NCLOB.
A subquery cannot be immediately enclosed in a set function.
The BETWEEN operator cannot be used with a subquery. However, the BETWEEN >operator can be used within the subquery.
click here for more information about sub queries.