0

I have two tables:

student

Columns

first_name
last_name

teacher_accounts

Columns

fname
lname

I have an input form that takes the data entered into an input field, and passes it through the following SQL query:

select * from student WHERE first_name like '%$q%' OR last_name like '%$q%'

However, I want to now join the teacher_accounts table to see if the name entered possibly matches what is entered into the input field. Essentially I need an SQL statement that checks the teacher_accounts and student table against the data entered into the input field.

Thanks for any help!

UPDATE

I need to mention that I'm outputting this data using mysql_fetch_array, so I need a way for the script to discern whether or not it needs to pull from the teacher columns or the student columns.

    while($row=mysql_fetch_array($result)) {
echo "<a href='?fname=".$row['first_name']."&lname=".$row['last_name']."'>".$row['first_name']." ".$row['last_name']."<br />";
1

2 Answers 2

2
SELECT first_name, last_name, 'student' as `type`
FROM   students
WHERE  first_name like '%<your string>%'
   OR  last_name  like '%<your string>%'

UNION

SELECT fname, lname, 'teacher' as `type`
FROM   teacher_accounts
WHERE  fname like '%<your string>%'
   OR  lname like '%<your string>%'

Refer to: http://dev.mysql.com/doc/refman/5.6/en/sql-syntax-prepared-statements.html for how to prepare statements and pass values safely using placeholders.

0
1
SELECT * 
FROM (
SELECT
first_name AS `first`,
last_name AS `last`,
'student' AS `typeofaccount`
FROM Student
UNION
SELECT
fname AS `first`,
lname AS `last`,
'teacher' AS `typeofaccount`
FROM teacher_accounts
) AS u
WHERE 
first like '%$q%'
OR last like '%$q%'
5
  • Hi, I'm having errors with this syntax. it said all derived tables require aliases. Feb 26, 2012 at 20:56
  • @zackt147: sorry I did not test it. I've edited the answer, please try it now Feb 26, 2012 at 20:57
  • This syntax has pros and cons. pros: you are filtering in one place, which might make it easier to edit in the future. cons: it might be a little slower because you're pulling all records from both tables twice (first to give it an accounttype, then applying the filter). I doubt the tables would be all the large, so longer query time might be minimal.
    – vol7ron
    Feb 26, 2012 at 21:38
  • The syntax above might have errors. I'm not sure if MySQL requires aliases to be wrapped in ``, but the fields probably need to be separated by commas (,).
    – vol7ron
    Feb 26, 2012 at 21:43
  • @vol7ron: syntax fixed, thank you! Also, I'm not sure what (if any) optimizations MySQL would use in the execution plan. I would probably choose the syntax you propose in your answer if the union between the two tables is only used in this query, the one I propose if there are other queries that need this union (in which case I would turn the inner query into a VIEW). Feb 26, 2012 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.