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.
Related
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;
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')
I have two tables first and second.
first Table:
ID NAME ADDRESS
1 test testing address
2 test1 testing address
3 test2 testing address
4 test3 testing address
second Table:
T_ID Partner_id date
1 2 12345678
3 4 32164584
If input T_id is given. Then corresponding Partner_id is taken and it is compared with the ID from first table and corresponding row should be selected.
Can you please tell me.
In php I can write this with two queries but I want it to be in a single query.
Queries in php:
$q=mysqli_query($conn, "SELECT Partner_id from second where T_ID=1");
$qa=mysqli_fetch_array($q);
$w=mysqli_query($conn, "SELECT * from first where ID=$qa[0]");
But, how to combine this two statements?
The specified result can be returned using a query with a join operation.
SELECT f.*
FROM first f
JOIN second s
ON s.Partner_id = f.ID
WHERE s.T_ID=1
Note that there is a potential for this to return more rows that the original, if the first query returns more than one row. (That is, we don't assume that T_ID is unique in second, or that every row with a given T_ID value will have identical values for Partner_id.)
There are other query patterns that will return an equivalent result, but the join operation is the normative pattern.
try to use union ,see this this link
where The SQL UNION operator combines the result of two or more SELECT statements.
SELECT * FROM first WHERE ID IN( SELECT Parent_id FROM second WHERE T_ID = 1 )
or
SELECT * FROM first WHERE ID = ( SELECT Parent_id FROM second WHERE T_ID = 1 )
SELECT * FORM first_table AS f
JOIN second_table AS s
ON s.parent_id =f.id
WHERE s.T_ID = $id
I am performing MySQL functions using PHP for my webpages.
I have got a table
table name -> users
uname oname
john alex
john murray
john joseph
john ray
and another table
table name -> more_info
fname lname more
alex some name some info
murray // //
joseph // //
ray // //
What I am trying to do is to run a query
Retrieve all the oname column values which are matched with uname john
This statement successfully retrieves the oname column values if uname = john
SELECT oname FROM users WHERE uname = 'john '
Now I want a statement something like this, based on the previously retrieved oname column values
SELECT * FROM more_info WHERE fname=oname // previos ones
I guess its Join orinnerJoin but somehow I am unable to get the correct syntax of it or any other simple query to do this.
My approach
I thought to retrieve the oname column values first and then inside a for loop, based on number of rows returned run a query for each column, but this approach eats up my server.
Try this query,
SELECT * FROM `more_info` `i` INNER JOIN `users` `u` ON `i`.`fname`=`u`.`oname` WHERE `u`.`uname`="john"
I think the following query wll help u.
SELECT * FROM `more_info` WHERE `fname` = (SELECT `oname` FROM `users` WHERE `uname` = "john")
You can merge your two queries into one by a simple INNER JOIN
SELECT * FROM more_info mi
INNER JOIN users u ON mi.fname = u.oname
WHERE u.uname = 'john'
If there is some chance of null values in more_info table, then you should use LEFT JOIN instead of INNER JOIN. Otherwise it is not necessary.
SELECT * FROM users A
INNER JOIN more_info B
ON A.oname = B.fname
WHERE uname = 'john'
See the MySQL documentation on the JOIN syntax for details.
I'm very new with SQL and need assistance on how I can accomplish this task using the correct query.
I have 2 tables that I need to use. Table "TB1" has:
id Name
1 bob
2 blow
3 joe
table "TB2" has:
compid property
1 bob
2 blow
I am trying to get which compid is missing in "TB2" and insert it from "TB1"
the query I am doing is:
SELECT id, name from TB1, TB2 where id <> compid
what I get is 2 ouputs of Id 1, and 2, and 3 outputs from id 3. by using php:
for($i=0;$i <= mysql_num_rows($comp)-1; $i++)
{
echo mysql_result($comp, $i, 0)."<br>";
}
and I expected the ouput 3 but instead got this:
1
1
2
2
3
3
3
I understand its comparing all the rows within the table but is there a way to achieve what I am looking for?
Thanks for your time.
You are performing an implicit Cartesian JOIN which results in every row against every other row. You need to specify what attribute JOINs the two tables.
Using implicit syntax (not recommended):
SELECT id, name
FROM TB1, TB2
WHERE id <> compid
AND TB1.Name = TB2.property <-- Column join
Using explicit syntax:
SELECT id, name
FROM TB1
JOIN TB2
ON TB2.property = TB1.Name <-- Column join
WHERE id <> compid
To accomplish your goal you would need something along the lines of:
SELECT TB1.id, TB1.name
FROM TB1
LEFT JOIN TB2
ON TB2.property = TB1.Name
WHERE TB2.compid IS NULL
See it in action
It's best practice to always alias the columns you select to prevent ambiguity.
To select it you can do:
SELECT *
FROM TB1
WHERE id NOT IN (
SELECT compid
FROM TB2
);