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;
Related
I have a simple table in phpmyadmin called name with only 1 column id which is look like this
id
--------
a
b
a
a
a
a
b
Now i want to show most used id on my PHP page.
Here is my code:
$sql_a = mysql_query(SELECT id FROM name WHERE id='a');
$count_a = mysql_num_rows($sql_a);
$sql_b = mysql_query(SELECT id FROM name WHERE id='b');
$count_b = mysql_num_rows($sql_b);
if($count_a > $count_b)
{
$most_used_id = "A";
}
else
{
$most_used_id = "B";
}
echo "<h1>MOST USED ID IS $most_used_id</h1>";
Currently I have only 2 types of id but in future I will have multiples (maybe 300+) id's, so is there a way to make query dynamic and get only most used value
That's very redundant code. You'd be better off with
SELECT id, count(id) AS cnt
FROM name
ORDER BY cnt DESC
GROUP BY id
LIMIT 1
that'll give you the "most popular" id value, and just how popular it is. If you need to get the count of all the ids, then remove the limit line.
Try the following query:
select id, count(id) cnt
from name
group by id
order by cnt desc
limit 1
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')
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
i am trying to run this query. but it shows either 1 row or give error query result having more than 1 row.
select * from abc where id=(select nameid from xyz where name like '%abc%')
help me.
i am executing a query where i get name, father name and schoolid from table abc. now i want school name from table2 by running another query in it but in all time it shows 1
Use the IN to fetch the more than 1 row:
select * from abc where id IN (select nameid from xyz where name like '%abc%');
EDIT:
select a.*, x.school_name from abc a left join xyz x on a.id = x.nameid where name like '%abc%';
try this with IN to get more then one row
select * from `abc` where id in (select nameid from xyz where name like '%abc%')
This should be
select * from abc where id IN (select nameid from xyz where name like '%abc%')
Use IN mysql clause.
SELECT * FROM abc WHERE id IN (SELECT nameid FROM xyz WHERE name LIKE '%abc%')
because where condition only check for one single value but you were comparing it with array of data returned by subquery.
What you were doin is
if($value = array(1,2,3))
which will always return false so you need to use something which enable you to look into array and give the result.
In other word comparison will only perform on once value like one = one not on one = array('one',two)
so IN clause will do that in MySQL.
I'm writing a simple URL rotator for a client and they want the URLs to rotate according to the oldest one that was previously displayed.
My columns are very simple:
url_id | company_id | url | last_clicked
I want to fetch a single row where the company_id is passed in and the last_clicked is the minimum of all records matching the company_id.
It should also select a random url_id if all last_clicked values are empty.
I assume this can be accomplished with a GROUP BY and HAVING but I can't seem to get the query to return anything.
I have this:
$last = $this->db->fetchOne ("SELECT url_id FROM
{$this->prefix}urls GROUP BY company_id HAVING MIN(last_clicked)
WHERE company_id='$company'");
This will fetch the row where last_clicked is smallest, or at random if they are all NULL:
SELECT url_id FROM urls
WHERE company_id = $company
ORDER BY last_clicked, RANDOM() LIMIT 1;
An index based on company_id and last_clicked would greatly help:
CREATE INDEX urls_ndx ON urls(company_id, last_clicked);
SELECT url_id FROM
TABLE
WHERE company_id='$company'
AND last_clicked = ( SELECT MIN(last_clicked)
FROM TABLE WHERE company_id='$company' )