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
Related
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);
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);
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')
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.
My two tables look like this:
TABLE1 TABLE2
+--------------------+ +--------------------+
|field1|field2|field3| and |field2|field4|field5|
+--------------------+ +--------------------+
I am already running a SELECT query for TABLE1, and assorting all of the data into variables:
$query = "SELECT * FROM TABLE1 WHERE field2 = 2";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
if((!is_bool($result) || $result) && $num_rows) {
while($row = mysql_fetch_array($result))
{
$field1 = $row['field1'];
$field2 = $row['field2'];
$field3 = $row['field3'];
}
}
What I want to do is get the data from 'field4' on TABLE2 and add it to my variables. I would want to get field4 WHERE field2 = 2
SELECT
t1.*,
t2.field4
FROM
TABLE1 AS t1,
TABLE2 AS t2
WHERE
t1.field2 = 2
AND
t1.field2 = t2.field2
You want to join the two table which can be done explicitly (using the JOIN operator or one of its variants) our implicitly (by SELECTing from multiple tables directly).
From your description, I suppose that you want to do the join where field2 in the two tables have the same value (2).
If TABLE2 will not always provide a valid join candidate but you still want the data from TABLE1, you should use a LEFT JOIN (giving NULL for field4 where nothing matched):
SELECT
t1.*,
t2.field4
FROM
TABLE1 AS t1
LEFT JOIN
TABLE2 AS t2
ON
t1.field2 = t2.field2
WHERE
t1.field2 = 2
http://en.wikipedia.org/wiki/Join_%28SQL%29
You need to use a JOIN:
SELECT TABLE1.*,TABLE2.field4 FROM TABLE1 JOIN TABLE2 ON TABLE1.field2=TABLE2.field2 WHERE TABLE1.field2=2;
hi try the following as ur query.
It is not preferred all the time to use * in the select query
SELECT T1.field1,T1.field2,T1.field3, T2.field4 FROM TABLE1 AS T1 INNER JOIN TABLE2 AS T2 ON T1.field2=T2.field2 WHERE field2 = 2