I have word which is dynamic "sticky9 uk". It might be anything.
I want to find similar record based on two columns
e.g
my_word = "sticky9 uk"
And in my database
Coloumn1 : company_name = "sticky9"
Coloumn2 : contact_person = "sticky9.com"
select * from your table where Coloumn1 like '%<Your Word>%' or Coloumn2 like '%<Your Word>%'
Related
I have a MySQL DB containing a table named "products".
This table contains a Json Data_type column named "values".
I would like to find the path to extract a specific value :
select JSON_EXTRACT(values, '$.COD')
from products
where id = '1'
returns :
"COD": {"<channels>": {"<locales>": "3699999999999"}}
And what I want is "3699999999999".
It is pretty obvious that my path is not the good one, but I can't find the solution.
Thanks for your help !
You can try
SELECT JSON_EXTRACT(`values`, '$.COD.<channels>.<locales>') FROM products WHERE id = '1';
For more details please refer mysql-for-your-json
I'm currently working on an API using node.js and node-mysql.
I start off by grabbing all the parameters that the client sends over like this:
var userId = req.query.userId;
var carType = req.query.carType;
var hasOwnership = req.query.hasOwnership; (optional parameter)
What I would like to do is make a statement so that if the client sends the hasOwnership parameter with the request, my statement will return all the cars that belong to him with the specific car type. Similar to this:
SELECT * FROM cars WHERE car_type=? AND owner_id=?, [carType, userId];
Otherwise it will just return the all the cars that match the car type:
SELECT * FROM cars WHERE car_type=?, [carType];
Is it possible to accomplish something like this in a single query?
Thanks in advance!
You can phrase the query as something like this:
SELECT *
FROM car
WHERE (? is null or car_type = ?) AND
(? is null or owner_id = ?);
Note that this version takes each parameter twice, unless you use named parameters.
A dirty quick solution would be to use an if then else.
if hasOwnership has value the use query #1 else use query #2
Let me start by saying that I am new to mysql and have searched on this site and others for a solution that will work for me. I've gotten fairly close (I think), but can't seem to find a solution.
I'm working on a Joomla site using RSForms software. I need to search a table where one row contains FirstName, another row contains LastName, and another row contains Email. Ultimately, I am looking for SubmissionId that is common to all 3. SubmissionId is a number generated by some form software that groups all of the form submission's elements by this id. I need to search for this id that is common to all 3. Each of the 3 elements may contain duplicates of the search. For instance, searching for the first name "John" will likely produce multiple results. I want to find the SubmissionId that matches "John" and last name "Doe" and email "johndoe#gmail.com" so I can use that to look up other information. It is also possible that there could be multiple matches for "John", "Doe", and johndoe#gmail.com".
I've tried many variations of the following (with/without ANY) and putting the results into an array and counting through each element. I've only had success in acquiring the first instance of "John".
$getresults = $db->setQuery("SELECT SubmissionId FROM my_table_values WHERE FormId = '$formid' AND FieldValue = '$fname' AND SubmissionId = ANY
(SELECT SubmissionId FROM my_table_values WHERE FormId = '$formid' AND FieldValue = '$lname' AND SubmissionId = ANY
(SELECT SubmissionId FROM my_table_values WHERE FormId = '$formid' AND FieldValue = '$email' ))");
---SubmissionValueId---|---FormId---|---SubmissionId---|---FieldName---|---FieldValue---
---------18192---------|-----20-----|-------5462-------|-----Email-----|---johndoe#gmail.com---
---------18193---------|-----20-----|-------5462-------|-----FName-----|---John---
---------18194---------|-----20-----|-------5462-------|-----LName-----|---Doe---
One possibility is to use a query that joins my_table_values with itself, even two times, to create something that can then be checked. For instance ... (fair warning: extemporaneous coding) ...
select A.submission_id from my_table_values A inner join my_table_values B using (SubmissionID) inner join my_table_values C using (SubmissionID) where (A.formID="email" and A.fieldValue= "foo#bar.com") and (B.formID="lastname" and B.fieldValue= "bar") and (C.formID="first name" and C.fieldValue="bletch")
You will need to examine that query to see (a) if the SQL processor accepts it, and (b) what sort of execution-plan it came up with. Preferably, all of the fields should be indexed. You need to see that it plans to use the indexes each time to narrow the list of possibilities, and that it plans to use the most-selective field (e.g. "email") first.
(The using (fieldname) syntax is specific to MySQL. You might need to use on to do the same thing.)
$getresults = $db->setQuery("
SELECT
MAX(IF(FieldValue = '$fname',SubmissionId, NULL) as fnameId,
MAX(IF(FieldValue = '$lname',SubmissionId, NULL) as lnameId,
MAX(IF(FieldValue = '$email',SubmissionId, NULL) as emailId
FROM my_table_values
WHERE FormId = '$formid'
GROUP BY FormId");
I have a drupal site with the search option. If user enters the search keyword, i need to compare that with more than one columns and display the records.
I have tried the following query
$search = 'test';
$sql_query = db_select('logoinfo', 'l')->fields('l');
$or = db_or();
$or->condition('search_field', '%'.db_like($search).'%','LIKE');
$or->condition('companyname', '%'.db_like($search).'%','LIKE');
$sql_query->condition($or);
$selectlogos = $sql_query->execute();
It displays all the records matching the search keyword with the order of auto increment Id asc.
But i want to display the records first which is having both search_field and companyname matches with the keyword, after that other records which is matches with either companyname or search_field. Please advise to achieve this.
Since orderBy requires a field name and can't order by an expression, you'll need to use addExpression to get an alias and then order by that alias. The expression in my example will return 0 if the value is not in both fields and 1 if it is in both fields. As far as I know this should be standard SQL, but it may vary on different database backends; so the expression may need to be adjusted depending on the database you are using.
<?php
$search = 'test';
$sql_query = db_select('logoinfo', 'l')->fields('l');
$or = db_or();
$or->condition('search_field', '%'.db_like($search).'%','LIKE');
$or->condition('companyname', '%'.db_like($search).'%','LIKE');
$sql_query->condition($or);
$safe_search = db_like($search);
$ex_alias = $sql_query->addExpression("l.search_field LIKE '%$safe_search%' AND l.companyname LIKE '%$safe_search%'");
$sql_query->orderBy($ex_alias, 'DESC');
$selectlogos = $sql_query->execute();
?>
I have one table named task_assignment.It has following 6 fields named as:
testId,quesId,evaluatorId,studId and marks
Actually this table is used to store marks for each test including each evaluators marks for each students by question id wise.
I have testId=1, quesId=Q1 and studId=S1 as a input. So, i want to get the following information in the select query.ie,Both evaluators(E1,E2) marks for the given input.
The sql query don't written more than one row for this...I want query output is :20,15 in a single row.
Please guide me to get out of this issue...
I think you won't be able to get your desired output 20, 15, since there is only one record which satisfies your criteria testId = 1, quesId = Q1, studId = S1.
But to answer your question, here's my query:
SELECT GROUP_CONCAT(marks)
FROM task_assignment
WHERE testId = 1
AND quesId = 'Q1'
AND studId = 'S1';
I've tried it in SQL Fiddle.
EDIT 1
If you want to parse the output of the query in your C# code to store them in separate variables, you can use the Split function:
string marks = "20, 15"; //Suppose that this value came from database
int mark1 = Convert.ToInt32(marks.Split(',')[0]);
int mark2 = Convert.ToInt32(marks.Split(',')[1]);
The code is still error-prone depending on the value of the marks variable, just make sure you have validated the value.
This might be unrelated to the question, but still to help you on your task, that's my answer.