Selecting using MATCH-AGAINST and IN in MySQL query? - php

I have the following query in PHP:
$titlematch = mysql_query("SELECT `item_id` FROM `items` WHERE `item_id` IN ($matches_s) AND MATCH (`eng_name`) AGAINST ('$query') IN NATURAL LANGUAGE MODE AS score;");
$titlematch2 = mysql_fetch_assoc($titlematch);
In plain words, I want to select the item_id from the table items where
the item_id value can be found within the array $matches_s, and
the string stored in eng_name can be found within the string $query
and then output a 2-dimensional array, where the second-level array contains the item_id and score (relevance) elements.
I have tried switching the order of which criteria comes first, or using brackets. Here are the results I get:
var_dump($titlematch); // returns bool(false)
var_dump($titlematch2); // returns NULL
Question is 'What am I doing wrong? Do I have to write this differently to other AND statements?

Related

Count non empty rows mysql

How to count non empty rows from 'naziv_operacije'? You can see table on the picture
If the rows are empty strings or null, the below should do it:
SELECT COUNT(*)
FROM `TEHN`
WHERE `naziv_operacije` IS NOT NULL OR `naziv_operacije` != '';
Try this:
select count(column_name) from naziv_operacije;
Replace column_name with the column which should not be empty
The documentation of COUNT() explains:
COUNT(expr)
Returns a count of the number of non-NULLvalues ofexprin the rows retrieved by aSELECTstatement. The result is aBIGINT` value.
COUNT(*) is somewhat different in that it returns a count of the number of rows retrieved, whether or not they contain NULL values.
If by "empty" you mean NULL then COUNT(naziv_operacije) is all you need.
Run this query to see how it works:
SELECT
COUNT(naziv_operacije) AS not_empty,
COUNT(*) AS all
FROM TEHN

MySQL FIND_IN_SET returning empty

I have a query where I am trying to find a matching tag from a comma separated value in a db Table, the value in the database may look like this:
"Tags":"all, kids, family, cqr, october"
and my query right now looks like this:
$rows = $db -> select("
SELECT `FriendlyId`,`Name`,`Description`,`Tags`,`BeginDate`,`EndDate`
FROM `Pages`
WHERE `SiteId` = '569e435701395'
AND `IsActive` = '1'
AND `BeginDate` IS NOT NULL
AND FIND_IN_SET('kids', Tags)
ORDER BY `BeginDate` ASC");
Unfortunately, this is returning an empty result even though I know there are rows in the db that match the query. This is my first time ever using FIND_IN_SET so am I doing something wrong?

mysql query for selecting data in multiple columns

I have table with 10 columns and I want to check input value in where clause of the MySQL query.
I want to do something like this. But, when I use this query I am getting an error.
for example :
SELECT * FROM user_data
where poll_title='$poll_title'
and '$voter' IN (user_vote_1,user_vote_2,user_vote_3...user_vote_10)
order by idpoll ASC
user_vote_1 to 10 (value is null'ed in the database) and I want to retrieve only that rows from a column which have $voter value.
I think you need this comparison (Not Sure OfCourse) :-
SELECT * FROM user_data
where poll_title = "$poll_title"
and (user_vote_1 = "$voter"
OR user_vote_2 = "$voter"
OR user_vote_3 = "$voter"
OR user_vote_4 = "$voter"......OR user_vote_10 = "$voter")
order by idpoll ASC
If I've understood what you want to do - return only the column with the value - then would coalesce do the job? This assumes that the value in user_vote_n will either match the value you're looking for or be null, since coalesce returns the first non-null argument.
(untested)
select coalesce(user_vote_1, user_vote_2, user_vote_3, ) as UserVote from user_data
where coalesce(user_vote_1, user_vote_2, user_vote_3, ) = '$voter';
That aside, this looks like a structure that could do with normalising - a single 'user_vote' column and a single 'user_vote_number' column.

I have a sql query where I have use a list of id inside IN and I want my result sorted as per my list

My query is
SELECT *
FROM `wp_patient_bill`
WHERE `bill_code` !=''
AND `is_billed` = 'Y'
AND `charged` = 'N'
AND `id` IN (97,419,631,632,633,422,635,421,35,799,60,423)
And I want the record array will be sort according to my list i.e. ID
'97,419,631,632,633,422,635,421,35,799,60,423'
. I am using php. Please suggest me the optimize way to do it.
Here is another solution:
SELECT *
FROM `wp_patient_bill`
WHERE `bill_code` !=''
AND `is_billed` = 'Y'
AND `charged` = 'N'
AND `id` IN (97,419,631,632,633,422,635,421,35,799,60,423)
ORDER BY FIELD(`id`,97,419,631,632,633,422,635,421,35,799,60,423)
Reference: SQL order by sequence of IN values in query
Assuming your list of IDs is in an array
Run the query
Loop through the results, storing each row of results in an associative array keyed on the ID $data["{$row['id']}"] = $row;
Loop through your list of IDs, and use the ID to key into the associative array to get the data. This will be in the order of the IDs you want.
Add an "ORDER BY ID" statement at the end of the query.

MySQL To Multidimensional Array

Say, I have a mysql table with columns like:
f_name, l_name, score1, score2, score 3
Then I want to create a PHP Associatiove Array from a query to get something like:
$array = ('name1'=>65, 'name2'=>45, 'anothername'=>23);
the scores e.g 65 is obtained by adding score1, score2 and score3 and name is obtained from f_name and l_name.
First of all what format of array you have specified is single dimension associative array.
You can use the following query:
SELECT CONCAT(FNAME,' ',LNAME) AS NAMES,
SCORE1+SCORE2+SCORE3 AS SUMS FROM EXAMPLE WHERE UID=1;
This query will produce result with two columns - NAMES and SUMS which can then be used to create an associative array through php.

Categories