Correct SQL syntax ( PHP and MySQL Combo) - php

i have this query :
SELECT
CONCAT(CT.FIRSTNAME,' ', CT.LASTNAME) AS NAME,
T.TYPE_OF_VISITOR_NAME AS TYPE_OF_VISITOR,
I.INDUSTRY_NAME AS INDUSTRY
FROM
COMPANY CY, CONTACTS CT, INDUSTRY I, TYPE_OF_VISITOR T
WHERE
CY.INDUSTRY_ID = I.INDUSTRY_ID
AND CY.COMPANY_ID = CT.COMPANY_ID
AND CT.TYPE_OF_VISITOR = T.TYPE_OF_VISITOR_ID
AND '$searchType' LIKE '%$searchString%'
where $searchType is a value from a combo box which contains 2 values :
NAME (this is not recognized in the database as long as i use CONCAT(CT.FIRSTNAME,' ', CT.LASTNAME) the problem is that when user picked COMPANY_NAME i will have wrong syntax)
COMPANY_NAME
and $searchString is an input box for search.
in the query above. when user searches for anything under COMPANY_NAME type it works. the problem is that i want to have a search option also for name(first name and last name). unfortunately the columns in my database is FIRSTNAME and LASTNAME. which i concatenated in the select statement.
Question :
What is the right way to achieve the search for FIRSTNAME and LASTNAME without affecting the result when user wants to search for COMPANY_NAME
As much as possible i want to do this using one query only. Any help would be much appreciated. Thanks

Instead of NAME, the combo box value should be
CONCAT(`CT`.`FIRSTNAME`,' ', `CT`.`LASTNAME`)
combo box values :
1. CONCAT(`CT`.`FIRSTNAME`,' ', `CT`.`LASTNAME`)
2. `COMPANY_NAME`

Related

How to return the name of a column after mysql LIKE search

I am trying to do a google like search function on my customer database.
so when you type you get drop down suggestions with the word u searched for highlighted.
i have 2 tables:
customers and addresses (because some customers have multiple addresses)
here is the search query and it works perfectly:
$query = "
SELECT id FROM
customer
WHERE
name LIKE :search OR
surname LIKE :search OR
email LIKE :search OR
CONCAT(name, ' ', surname) LIKE :search
UNION
SELECT cust_id FROM
addresses
WHERE
CONCAT(address, ' ', town, ' ', postcode) LIKE :search
";
:search is obviously whats been searched for. It returns the customer id and from then i can display dropdown suggestions as they type. (done thru ajax)
What i am TRYING to achieve is rather than showing ALL the information(name surname email phone address town postcode etc) in the drop down suggestions, i would like to show The name and surname and whatever it is they are clearly searching for.
For example lets say on the database a customer is called:
Foo Bar.
tel: 0123456789
email: foo#bar.com
In the addresses table he has an address of 6 pack lane, petersville, EX23 6GH.
So as i type "pack" into the search field i want a drop down suggestion to come up with "Foo Bar, 6 Pack Lane"
But to do this i need to find out which column "Pack" refers to.
At the moment i am only returning the id because i dont know which piece of data is going to be searched for.
so is there a way of returning the name of the column that the LIKE query is returning?
many thanks

Show full name from two columns in mysql table

I am selecting a full name from two columns in mysql table but the input takes only one order to return the information.
firstname | lastname
Amaj | Ato
With this data I want to select the full name (Amaj Ato or Ato Amaj) from one text box when the user enters Amaj Ato or vice versa (Ato Amaj).
This is what I have tried so far
`SELECT * FROM `table_name` WHERE concat_ws(' ', 'firstname', 'lastname') like '%$fullname%'; `
This is able to select the firstname and lastname in only one order. For instance if the user enters Amaj Ato the query gives the full name but when the user enters the firstname after the lastname ( like Ato Amaj), the query returns empty. I want the user to get the full name in any order he enters the names. Thanks for helping.
You need to "tell" the query what you are looking for, it doesn't magically know that names could be switched. To do this, you can simply add an OR specifying the other valid case:
SELECT * FROM `table_name`
WHERE concat_ws(' ', 'firstname', 'lastname') like '%$fullname%'
OR concat_ws(' ', 'lastname', 'firstname') like '%$fullname%'

Displaying searched record for a query using 'like' at the top in mysql

I am writing a search query which makes a search based on first name and last name.
The query is like this:
select fname,lname
from users
where fname like fname like '%$name%' or lname like '%$name%';
if the user enters first name and last name in the search box,I have exploded it with a 'space' and the query goes like this
select fname,lname
from users
where fname like fname like '%$f_name%' or lname like '%$l_name%';
In the second query if I enter John Thomas,it shows me all the records with John or Thomas in first or last name,but the actual search result i.e John Thomas(exact match) is somewhere below in the results.(If I have 100 results its on the 60th position).
How can I modify the query to achive this or do I have to handle it programatically? i.e Check the result array and match the check box value for its presence and display it fist.
Try:
SELECT fname, lname FROM users
WHERE (fname = '$f_name' AND lname = '$l_name')
OR (fname LIKE '%$f_name%') OR (lname LIKE '%$l_name%');
And if you need to order then ORDER BY lname, fname in the end of the query, depending on what you need to order by of course.
ADD in query
ORDER BY `fname`
You can testing mysql sorting
More information here ==> http://dev.mysql.com/doc/refman/5.0/en/sorting-rows.html

Making a general search box using php and mysql to search through several tables

I have 2 tables namely: tbl_subscriber and tbl_faculty.
tbl_subscriber has fields namely:name,id_number,email,phone.
tbl_faculty has :f_name,l_name,email,phone.
I have a search box. I want to write a query that if I type a phone number / email / name / f_name / l_name in it then it should search both the tables to match the supplied entry and it should return all rows corresponding to that entry.
It is for sure that the supplied entry would be in either of the mentioned table (as I have made the tables like that)and not in both. What I mean is at one time only one table fields would match but the query should search both the tables simultaneously.
select x , y from tbl_subscriber
union all
select x , null as y from tbl_faculty
This might do the trick:
SELECT name,id_number,email,phone FROM tbl_subscriber
UNION
SELECT CONCAT(f_name, ' ', l_name) AS name, id_number, email, phone FROM tbl_faculty
You might need to do some casting for the name field, but it does at least get around the problem of the tables having different number of fields.

Finding out related data from tables

I have three tables in my mysql database which looks like following:
EDIT
The table "members" holds the name and code of some members . Now the table "Boss" contains the name of the member who has been selected as a Boss for an event. Each boss has at least one subordinate (please check the table subordinates).
Now what I am trying to do is find out the names and their points that Bon Jovi (pls check the table table:members) is associated with. When finding out there is a condition... if the "condition_type" from table:boss is 1 then the values from subordinates would be in the right side and if the "condition_type" from table:boss is 0 then the values from subordinates would be in the left side.
Now what I am trying to find out looks like following.
OK i have found a solution for you i am posting
$condition = 1;// Condition will be provided by you for selection. Could be zero
$data = array(
'members.name',
'subordinates.points',
'subordinates.event'
);
$this->db->select($data);
$this->db->join('members','members.code = subordinates.member_code','left');
$this->db->join('boss','boss.event = subordinates.event','left');
$this->db->where('boss.condition_type',$condition);
$this->db->where('boss.event = subordinates.event');
$this->db->get('subordinates');
Also i have event in the selection so that you recognize the difference of events the query will bring. Kindly test and see if it works ok for you. There might be a little difference in the column names. A good approach is to use like this for easy and understandable relationship between tables.
Table1 : members
Columns : id , code , name
Table2 : boss
Columns : id , event , member_code , condition_type
Table3 : subordinates
Columns : id , boss_event , member_code , points

Categories