Combine multiple column values in to single value in SQL select statement - php

I have an autocomplete field where user can enter lastname firstname middlename in the same order.
Using lastname firstname middlename (as a term) I have to show the autocomplete dropdown.
On my database I have 3 columns for firstname lastname and middlename. Now I have to compare 3 column values (asc_lastname, asc_firstname, asc_middlename) of same row with user input (term).
Here is the SQL query. Please correct my mistake.
$rs = mysql_query( "select asc_lastname, asc_firstname, asc_middlename from issio_asc_workers INNER JOIN issio_workers_asc_sc_list
ON issio_asc_workers.lname_fname_dob=issio_workers_asc_sc_list.lname_fname_dob where issio_workers_asc_sc_list.center_id='$cid'
AND issio_workers_asc_sc_list.center_user_type = '31' AND issio_workers_asc_sc_list.center_status <> 'deleted' AND
(issio_asc_workers.asc_lastname+' '+issio_asc_workers.asc_firstname+' '+issio_asc_workers.asc_middlename)
LIKE '". mysql_real_escape_string($term) ."%' ORDER BY issio_asc_workers.lname_fname_dob ASC LIMIT 0,10");
Is it possible to compare 3 column values at a time using a SQL query like
select *
from table
where (column1+''+column2+''column3) like 'this is good'
Sorry for my poor sentences.

Try this query:
SELECT * FROM your_table WHERE CONCAT(column1, ' ', column2, ' ', column3) LIKE '%this is good%'
Documentation for CONCAT

Related

PHP Search form in SQL database/table

I have a table with many columns
( A , B , C , D , E)
I have search form and it's works
$query = $pdo->prepare("
SELECT *
FROM Database
WHERE Name LIKE '%{$search}%'
");
On B column I have Names, on A column I have numbers.
I want to search a name from column B and to display it only if A = 0.
SELECT *
FROM Database
WHERE Name LIKE '%{$search}%'
AND A = 0
The SQL query would be the following, using '?' on LIKE to use the prepared statement logic, it is replaced on the next line for the variable $search.
$query = $pdo->prepare("
SELECT *
FROM Database
WHERE B LIKE '%?%' AND A=0
");
$sth->execute(array($search));

Ordering by a value that is not in the database where selecting from

how would i go about ordering by a value that is not in the table where i am selecting from, in this instance the value $count1 is not in the table search.
count has the same identifying id as that of the thing it is being reffered to in the other table, this is where count1 is grabbed
$q = $db->prepare("SELECT COUNT(rating) FROM ratings WHERE id='$id' AND rating = 'd'");
$q->execute();
$count1 = $q->fetchColumn();
$query = "SELECT * FROM search WHERE title LIKE '$each' ORDER BY '$count1'"
$query = $db->prepare($query);
$query->execute();
that is from ratings, how would i go about ordering the entries like that, so that they are based off the number of count1 and are decided, i might have to implement something like
$query = "SELECT * FROM search WHERE title LIKE '$each' AND id = '$id' ORDER BY '$count1'"
$query = $db->prepare($query);
$query->execute();
Possible Duplicate: Mysql order by specific ID values
Same thing here, you'll just output your $count1as a comma separated string and add it in the SQL query as ORDER BY FIELD(COUNT,___comma_sep_string___)
ratings is a table, not a database. You can join tables or use subqueries to get the desired result, without having to make multiple queries.
You haven't described how the FOREIGN_KEY is set up in the ratings table, but assuming you have something ratings.search_id, this should work:
SELECT search.*, (SELECT COUNT(rating)
FROM ratings
WHERE ratings.search_id = search.id
AND rating = 'd'
) AS rating_count
FROM search
WHERE title LIKE '$each'
ORDER BY rating_count

using search firstname and last name mixed php query

I have a SQL query command. The problem is when I type the whole name of a person it won't appear. For example if I search for "ermel", it will show ermel. However, when I search for "ermel lopez" it will fail to output the query. Here's my query:
$query=mysql_query("select * from persons where firstname like '%$searchtext%' or lastname like '%$searchtext%' order by date desc
LIMIT $start,$per_page ");
I tried CONCAT, which works, but won't work on the executable:
SELECT *
FROM persons
WHERE CONCAT( firstname, ' ', lastname ) LIKE 'kaitlyn'
OR `LastName` LIKE 'pineda'
LIMIT 0 , 30
Try reversing the way you check your strings like this:
"select * from persons
where '$searchtext' like concat('%',firstname,'%')
or '$searchtext' like concat('%',lastname,'%')
order by date desc
LIMIT $start,$per_page "
This should result in a match on your searchtext containing either the firstname or lastname.
a LIKE without wildcards is an exact match!
you need
Like 'kaitlyn%'
I think your Sql Query is wrong
when you search Full name (ermel lopez)
Then from your query, you search it in both columns.
But whole text is not stored in a columns
Try this
$searchtext1="ermel";
$searchtext2= "lopez";
$query=mysql_query("select * from persons where firstname like '%$searchtext1%' or lastname like '%$searchtext2%' order by date desc
LIMIT $start,$per_page ");

How to search data in combination of field

I have a student table with first_name and last_name. when i apply the search on first name or last name it give me the correct record but when i search with complete name it does not return any thing. Here is my query
SELECT
`student_personal_info`.`spi_id` AS `sid`,
`student_personal_info`.`spi_first_name` AS `fname`
FROM
`student_personal_info`
WHERE
((spi_first_name like '%test developer%') OR (spi_last_name like '%test developer%')
ORDER BY
`spi_first_name` ASC
Is there any way to make it searchable with both field combination.
It will give record in case we enter first name .middle name and last name but don't fetch in case of firstname and last name not middle name.
SELECT `student_personal_info`.`spi_id` AS `sid` , `student_personal_info`.`spi_first_name` AS `fname`
FROM `student_personal_info`
WHERE (
spi_scholar_no LIKE '%amir hussain khan%'
)
OR (
CONCAT( spi_first_name, ' ', spi_middle_name, ' ', spi_last_name ) LIKE '%amir hussain khan%'
I have also try this but its working for exact match only.
SELECT `student_parents_info`.`spri_spi_id` AS `sid`
FROM `student_parents_info`
WHERE (MATCH (spri_first_name, spri_middle_name, spri_last_name) AGAINST ('Pramod Kumar Sharma*' IN BOOLEAN MODE ) )
ORDER BY `spri_first_name` ASC
LIMIT 0 , 30
Please give any other suggestions.
Try searching using concat()..
SELECT `student_personal_info`.`spi_id` AS `sid` , `student_personal_info`.`spi_first_name` AS `fname`
FROM `student_personal_info`
WHERE CONCAT( spi_first_name, ' ', spi_middle_name, ' ', spi_last_name ) LIKE '%test%'
ORDER BY `spi_first_name` ASC
It always works for me .
Try like this
SELECT `student_personal_info`.`spi_id` AS `sid`, `student_personal_info`.`spi_first_name` AS `fname`
FROM `student_personal_info`
WHERE
((spi_first_name like '%amir khan%') OR (spi_last_name like '%test develope%'))
ORDER BY `spi_first_name` ASC
you are missing ')'
Split your test develope to test and develope using php, then write query like this :
$first = 'test';
$last = 'develope';
$query = "SELECT `student_personal_info`.`spi_id` AS `sid`, `student_personal_info`.`spi_first_name` AS `fname` FROM `student_personal_info` WHERE
(spi_first_name like '%".$first."%') OR (spi_last_name like '%".$last."%')
ORDER BY `spi_first_name` ASC";
If you have more than two words, Split the words and create the dynamic where query.
Try with CONCAT:
SELECT
`student_personal_info`.`spi_id` AS `sid`,
`student_personal_info`.`spi_first_name` AS `fname`
FROM
`student_personal_info`
WHERE
CONCAT(spi_first_name, ' ', spi_last_name) LIKE '%test develope%'
ORDER BY
`spi_first_name` ASC
Do you receive a full name to perform the search with? or do you always get 2 separate (first and last name)?
If you get one full name string, change your query to concatenate first and last name, and use
GROUP BY HAVING(full_name LIKE %search_term%)
if you get two separate strings, just change your OR to AND between the first and last name LIKE tests.

Get Duplicate data from the Database

i want to fetch duplicate data from the database but the problem is:
i have userids in an array like :
1235, 1235, 5468, 84321, 1235
i used implode and to make that array in string in implement into the database like:
"select * from tbl_name where userid in ('" . $implode_arr . "') limit 0, 10";
which is give me the result like 1235, 5468, 84321 but i want all the data, solution for this is simply run the query in the FOR LOOP or else
but i want same because i add the pagination within the query.
I don't want any code because its already working but the problem is to add pagination Please help to resolve the problem :(
It sounds like you want to join in the user data into this query. You can do that using a LEFT JOIN like this:
SELECT *
FROM tbl_name
LEFT JOIN database_name.user ON database_name.user.id = tbl_name.userid
WHERE userid IN (...)
LIMIT 0, 10
This is assuming your users are stored in the user table in the database_name database, and that the user table has an id field which matches up to the tbl_name field userid. If your fields or table names are different, just change them in this query. In PHP that would be:
"SELECT * FROM tbl_name LEFT JOIN database_name.user ON database_name.user.id = tbl_name.userid WHERE userid IN ('" . $implode_arr . "') LIMIT 0, 10"
Note that to connect two databases, both must be accessible by your same connected user.
Try this it would also work as expected
select * from tbl_name where FIND_IN_SET(user_id,'1235, 1235, 5468, 84321, 1235
') limit 1,10
Check this to remove duplicates

Categories