Retrieve data from one MySQL table and use it in another one - php

I have two tables i use in jqgrid.
Table 1: id->0,1,2; state->1,1,0
Table 2: id->0,1,2,3; product->apple,banana,cherry,melon;
I want to find the id's which have state 1 and retrieve the corresponding product from the other table. Is there a proper way to do it?
$result below always returns zero. I am connected to the database successfully.
Here is the code:
$var = "SELECT id FROM table1 WHERE state = 1";
$result = mysql_query($var);
$grid->setSelect('order', "SELECT id, product FROM table2 WHERE id='$result' ");

You can run one query:
SELECT t2.id, t2.product
FROM table2 t2
WHERE t2.id = (SELECT t1.id FROM table1 t1 WHERE t1.state = 1);
If the subquery could conceivably return more than one result, then use in instead of =:
SELECT t2.id, t2.product
FROM table2 t2
WHERE t2.id IN (SELECT t1.id FROM table1 t1 WHERE t1.state = 1);

Related

How to take fields from two different table and checking the value with WHERE?

I am a beginner programmer and I am trying to select fields from different tables before checking the fields with WHERE clause in PHP. I understand that usually people get fields from different table like
SELECT t1.firstName, t2.lastName
FROM Table1 t1, Table2 t2
WHERE (t1.id = t2.id)
However, I am trying to do use the same field for WHERE clause like this:
$myid = $_GET['myid'];
SELECT t1.firstName, t2.lastName
FROM Table1 t1, Table2 t2
WHERE (t1.id = t2.id AND id = $myid)
What I really want is t1.id = t2.id = $myid for my where clause but this is giving me errors.
Below is my set of codes:
$result = $conn->query("SELECT ve.vendorname, ve.vendortype, ve.rebatepercentage,
ve.vendorimages, ve.details, vo.totalratings, vo.totalno FROM Vendors ve, vendortotalratings vo
WHERE ve.vendorid = vo.vendorid AND vendorid = " . $vendorid);
You don't need to check both columns. When you join confirm the fields you want match then you only need to check one of the columns, because both have the same value.
on t1.id = t2.id
where t1.id = 2
would match both where t1.id = 2 and t2.id = 2.
Additionally note that passing user input directly to a SQL query opens you to SQL injections. You should parameterize your query.
$myid = $_GET['myid'];
SELECT t1.firstName, t2.lastName
FROM Table1 t1, Table2 t2
WHERE (t1.id = t2.id AND id = ?)
then bind the $myid. How you bind depends on the PHP driver you are using. With PDO it can be passed directly to the execute as an array.
As user #chris85 has mentioned in the comments,
$result = $conn->query("SELECT ve.vendorname, ve.vendortype, ve.rebatepercentage,
ve.vendorimages, ve.details, vo.totalratings, vo.totalno FROM Vendors ve, vendortotalratings vo
WHERE ve.vendorid = vo.vendorid AND ve.vendorid = " . $vendorid);

Two database in single SELECT Statement

I want to select everything from two database: database1 and database2. Both database row id is equal to $user_id = $_SESSION['user_id']. How do i write that sql statement for query?
If they are on the same server you can just use the database.table notation:
SELECT t1.* FROM database1.table AS t1 WHERE t1.id = $id
UNION
SELECT t2.* FROM database2.table AS t2 WHERE t2.id = $id
off the top of my head

Mysql 2 tables use player_id with name from other table

I am new to php & mysql and I'm trying to make a script that gets the distance walked with the player's name. I can get the player's walked distance with his id, but the value for the player_id is in a different table.
It looks like this:
Table1: player_id | foot (walked distance)
Table2: name | player_id
So I want to use the name by the player_id in my table.
Code
You require a simple join.
SELECT Table1.foot, Table2.name
FROM Table1
INNER JOIN Table2
ON Table1.player_id = Table2.player_id;
You just need to join both these table.
Just try this code:
$query = "SELECT T1.*, T2.name
FROM table1 T1
LEFT JOIN table2 T2 ON T1.player_id = T2.player_id
ORDER BY T2.name ASC";
For more details of JOIN: Link
Let me know for more help.
You can use
$query = "select t1.player_id, t2.name, t1.foot
from table1 t1
join table2 t2 on t1.player_id = t2.player_id"
If you want to order the player names in alphabetical order then you can additionally use order by clause
$query = "select t1.player_id, t2.name, t1.foot
from table1 t1
join table2 t2 on t1.player_id = t2.player_id
order by t2.name"
Use left join in mysql.
Suppose if you have two tables use this
SELECT T1.*,T2.walked distance
FROM table1 T1
LEFT JOIN table2 T2
ON T1.id=T2.player_id;
Click Here For more example

Join tables on missing matched rows from the second table

I have two database tables - table1 and table2. For some records in table1 i have several rows connected in table2. For most of them i have 3 rows connected, but for some of them i have an extra row with a column value like table2.field='correct'. How can i join table1 and table2 if i want the result to return only the rows from table1 where there is no row in table2 with column value like table2.field='correct' connected to them ? Counting the number of rows from the second table( if num of connected rows < 4 or something like that is not an option).
I tried something like :
SELECT t1.* FROM table1 t1 LEFT JOIN table2 t2 on t1.id=t2.id_t1 WHERE t2.field IS NULL
but it did not work ofc. because i always have rows with value in field column. For each row in t1 that is connected to t2 i have record rows in t2 where t2.filed='name' and t2.field='type'. I need the rows from t1 that do not have a row connected to them in t2 where t2.field='correct'.
Use NOT IN
SELECT * from t1 WHERE t1.id NOT IN(SELECT id FROM t2 WHERE t2.field = 'correct')
$sql = "SELECT t1.* FROM table1 t1 CROSS JOIN table2 t2 on t1.id=t2.id_t1 WHERE t2.field IS NULL ";
SELECT t1.* FROM table1 t1 WHERE NOT EXISTS (SELECT t2.* FROM table2 t2 WHERE t1.id=t2.id_t1 AND t2.field = 'correct')

Double tables select show?

I have a problem...I have 2 tables, Table1 and Table2.
Table1:
id,int(11)
text,varchar(11)
Table2:
id,int(11)
agentid,int(11)
unique(id,agentid)
Table2 has many id and agent ids in it. The data for the id in table 2 came from table 1.
I have an agentid say $aid. This has many id's associated with it in table 2.
I am trying to get the set of text values out from table 1 associated with all ids which are related to agentid $aid from table 2.
Does that make sense?!
Any ideas?
select text from table1 where id in
(
select id from table2 where agentid = <your aid>
)
This will give you all text rows for given agentid.
The same can be done using join too -
select t1.text from table1 t1
inner join table2 t2
on t1.id = t2.id
where t2.agentid = <your aid>
select * from table1 as t1 inner join table2 as t2
on t1.id = t2.id
where agentid = $aid
The query:
$sql = "select t1.text from table1 t1, table2 t2 where t2.id = t1.id and t2.agentid = {$aid}";
Try not to include the $aid directly in the query, but escape it and/or use prepared statements with query parameters.

Categories