Search a query that matches multiple table fields - php

How to retrieve / output values that match multiple table fields.
SELECT name FROM table WHERE name IN ( 'Value1', 'Value2' );
My search query should take the following parameters : first_name and roll_number in user_table.
e.g. Retrieving a query on the following lines : Roy whose roll number is 5
I need to query this using a single query.

Consider writing the following sql statement:
SELECT name FROM table_name WHERE first_name IN ('Roy') AND roll_number = 5;
You can alternatively try the following query:
SELECT name FROM table_name WHERE first_name = 'Roy' AND roll_number = 5;

Select * from tableName
where roll_number =5
and first_name ='ROY'
--replace 5 and ROY with parameter

Related

Get items count by key name

I have these records in a MySQL DB. On a JSON type column.
Record1: {"images": ["image.jpeg", "image.jpeg"]}
Record2: {"images": ["image.jpeg", "image.jpeg", "image.jpeg"]}
Record3: {"images": ["image.jpeg"]}
How can I get the total count of images by the key name of the JSON property which in my case is 'images'?
I am trying to achieve the same thing as running this below query, but counting the array items from the images key.
SELECT
`field`,
count( * ) AS total
FROM
`table_name`
WHERE
`condition`
GROUP BY
`something`
which will give you this result:
field total
------------------
field1 5
field2 2
What I am trying to achieve:
field total
-----------------
images 6
Table structure
Table data
Try:
SELECT
`type`,
SUM(
JSON_LENGTH(`media`, '$.images')
) `media`
FROM
`table_name`
GROUP BY
`type`;
See dbfiddle.
ok sure You can use this query :
SELECT type,JSON_LENGTH(media) FROM `tabl4` group by type
check the execution from here : Demo

How to fetch value of primary key?

I have an SQL table where there are three columns: id, name, and group. I want to get the value of group from below example:
Id = 1
Name = abc
Group = 1
I need an SQL query from which I can get value abc on fetching group=1.
You should use something like this:
SELECT Name
FROM table_name_here
WHERE table_name_here.Group = 1
You can do if you want to strict filter your request using operator AND/OR.
SELECT id, name, group FROM TABLENAME
WHERE id = 1 AND name = "abc" AND group = 1;

Multiple Where clause using same columns SQL

I have table like this :
user_id field_id value
1 1 toto
2 1 tata
2 2 tata Job
user_id is the id of my users, field_id is the id of the information about the user (name, job, etc.) and value is the value.
I use a searchBar in HTML/PHP with 2 fields : 'name' and 'job'.
When someone search user using this two fields I need to retrieve the user id that matches these two conditions.
My question is :
How to get in SQL all user_id that match with two condition in one like this:
(field_id=1 and value=tata) AND (field_id=2 and value=tata's Job)
Thank you and have a good day !
SQL executes the WHERE logic for each row, not for the entire set. And fields cannot have more than one value. So, you need to use an OR statement instead of an AND to capture both conditions:
Select User_Id, Field_Id, Value
From YourTable
Where (field_id = 1 And value = 'tata')
Or (field_id = 2 And value = 'tata Job')
One method uses aggregation:
select user_id
from t
where (field_id = 1 and value = 'tata') or (field_id = 2 and value = 'tata''s Job')
group by user_id
having count(distinct field_id) = 2;
You can use IN statement, for example: Your table name is users
SELECT user_id
FROM users
WHERE field_id IN (1,2) AND `value` IN ('tata','tata\'s Job')

MySQL WHERE IN Returns only 1 record

I' am storing the profile friends ID in a string format comma delimited. When the run the Query, its gives 1 record whereas it should return with 2 records.
MySQL Query
SELECT * FROM PROFILES WHERE profile_id IN(SELECT profile_friends FROM PROFILES WHERE profile_id = '1')
Which gives this result (The results should be 2 records NOT 1 Record)
When I run the following Query, gives me two ID's that are in a profile_friends field.
SELECT profile_friends FROM PROFILES WHERE profile_id = '1'
Please use following Query
SELECT * FROM PROFILES WHERE FIND_IN_SET(profile_id,(SELECT profile_friends FROM PROFILES WHERE profile_id = '1'))
IN clause only use when you search value from integer field
But your field(profile_friends) is string so following clause you need to use.
FIND_IN_SET

SQL Select query where column like string with and without spaces

I have a SQL Select query in PHP where i need to lookup rows in a table where a column is like a string with and without spaces
for example, if my value in the database is ABC 123 and i search for ABC123 the query will be
$sql = SELECT * from table where col_name LIKE '%ABC123%' ";
but i need it to return the row where col_name equals ABC 123
and vice versa, for example, if the database value is 123ABC and i search for 123 ABC the query will be
$sql = SELECT * from table where col_name LIKE '%123 ABC%' ";
and i need it to return the row where col_name equals 123ABC
$sql = SELECT * from table where REPLACE(col_name,' ','') LIKE '%ABC123%' ";

Categories