So I have two tables in my phpmyadmin like
tabel1 and tabel2
in both tables, i want to select id = 2
so I have tried
mysql_query('SELECT * FROM tabel1, table2
WHERE id=2');
but not working plz give me some suggestions
You can use UNION ALL to accomplish what you want.
SELECT * FROM table1 WHERE id = 1
UNION ALL
SELECT * FROM table2 WHERE id = 1
Depending on your exact requirements, the query might be as easy as
SELECT *
FROM table1, table2
WHERE table1.id=2 AND table2.id=2
You are implicitly joining your tables for the condition of table1.id and table2.id being equal 2.
Use inner join and also read Manual
mysql_query('SELECT * FROM tabel1 as t1
inner join table2 as t2 on t1.id=t2.id
WHERE t1.id=2');
I think you have a typo there. You said you have table1and table2
Then your SQL statement should be
mysql_query('SELECT * FROM table1, table2 WHERE id=2');
instead of
mysql_query('SELECT * FROM tabel1, table2 WHERE id=2');
what was in your question.
Related
I know this involves JOINS but I can't seem to find a working solution to what I'm trying to do.
I have 2 custom tables :
table1 | table2
---------------------
id id
uid uid
track_id track_id
date date
art active
info
blah
blah2
First I want to select everything WHERE uid=55 AND active=1 from table2 :
$tracks = $wpdb->get_results( "SELECT * FROM table2 WHERE uid = 55 AND active = 1");
And then match the track_id from table2 with results from table1 so I can traverse the table1 data.
I know I can do it like this :
foreach( $tracks as $track ) {
$this_track = $track->track_id;
$results = $wpdb->get_results( "SELECT * FROM table1 WHERE track_id = $this_track");
// Do stuff here
}
But this is the part where it gets tricky...
I then want to ORDER the $results from table1 by date DESC from table2
And this is where I'm lost...
Effectively I want (pseudo code) :
$results = $wpdb->get_results( "SELECT * FROM table1 WHERE track_id = $this_track" ORDER BY date DESC FROM table2);
As well as that last bit, I know I can do this entire routine with JOINS to keep this all in one query and make it way more efficient but I just don't know how.
So just to be clear, my overall routine should be like this :
Get all instances of track_id from table2 where user_id=55 and active=1, then use those results to match the track_id to every result in table1 with the same track_id and then sort the results by date back over from table2
Psuedo code, I know it contains nonsense :
$finalresults = $wpdb->get_results( "SELECT * FROM table2 where uid=55 AND active=1 THEN SELECT * FROM table1 WHERE track_id = "the track_id from the first query" THEN ORDER BY date DESC FROM table2);
Try with this query
SELECT t1.* ,t2.date AS t2date, t2.active FROM table2 AS t2 INNER JOIN table1 AS t1 ON (t1.track_id = t2.track_id) WHERE t2.uid=55 AND t2.active=1 ORDER BY t2.date DESC;
Edit: Explanation of what this query is doing. and inverted the order of the tables retrieved in the query (this don't affect the final datatset, i did this to make to follow the logic of the explanation.
1.- Begin with retrieving all rows from table2 (theres is no specific reason because i used table2 over table1, I'm only following an logical order), using the criteria that you specified iud=55 and active=1
SELECT * FROM table2 WHERE uid=55 AND active=1;
2.- but as you said you need to expand the data retrieved in table2 with some information in table1, that's exactly what it is the directive JOIN made, and we are using INNER JOIN because this type of JOIN will show rows ONLY if data for the uid=55 is present on table1, if there is NO data for the uid=55 present on both TABLES then mysql wil show empty the recordset (0 Rows selected).
in the ON(...) part I specify which criteria mysql will use to compara both tables for match in this case will compare that track_id on table2 it is the same that the specified on table1, if this codition is met then mysql considers it as a match.
anly for convenience and because i'm adding a Second table i gave an Alias to each one t1 and t2.
then the query now seems like this
SELECT * FROM table2 AS t2 INNER JOIN table1 AS t1 ON(t1.track.id = t2.track_id) WHERE t2.uid=55 AND t2.active=1;
3.- but then raise a problem, both tables has rows with the same field names, and this is something that DBMS don't like in their queries, to avoid this situation in the query i only show the fields (id, uid and track_id) from one table in this case t1 (t1.*) and only show the fields that doesn't have this problem from t2 (t2.date AS t2date, t2.active). in this way mysql won't throw any error.
SELECT t1.* ,t2.date AS t2date, t2.active FROM table2 AS t2 INNER JOIN table1 AS t1 ON (t1.track_id = t2.track_id) WHERE t2.uid=55 AND t2.active=1;
4.- for the final step i specify to mysql that i want all found rows ordered descent by a field in the table2;
ORDER BY t2.date DESC;
then this criteria will be applied to the whole selected rows. and the final query has this form.
SELECT t1.* ,t2.date AS t2date, t2.active FROM table2 AS t2 INNER JOIN table1 AS t1 ON (t1.track_id = t2.track_id) WHERE t2.uid=55 AND t2.active=1 ORDER BY t2.date DESC;
if is not completely clear you can ask ...
Hello i searched on google but i could not find it(maybe wrong search terms) But i'm asking if there is a way to check if name1 from table 1 exists in table 2
So like
select name from table 1.
search in table 2 for the name from table 1
is this possible? if yes how?
~Kev (bad english = sorry)
Select name from table1 Inner Join table2 on table1.name = table2.name;
Depending on your structure this will give you all the names which exists both in table1 and table2 since the joining is done itself on the name
Perhaps something like this (untested)
SELECT name1 FROM tableA WHERE name1= (SELECT name2 FROM table2 WHERE .... )
You are asking about joins between 2 tables. To query all entries from table 1 that exists in table 2 you need next SQL:
SELECT * FROM table1 t1 INNER JOIN table2 t2 ON t1.name = t2.name
$s2="select * from trackyesttrackyest";
$q2=mysql_query($s2) or die($s2);
$row=mysql_fetch_array($q2);
$s="select * from <secondtablename> where rsname='".$row['rsname']."'";
$q=mysql_query($s) or die($s);
$row2=mysql_fetch_array($q);
please refer this link
click here
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
Its simple buddy.........
try out this...
SELECT * FROM table1 WHERE table1.name in (SELECT table2.name FROM table2)
I want to make my keyword search in two mysql tables. my tables don't have any identical column names. But I tried few queries, they didn't work for me.
Keyword IS 07731A0328
I tried this:
$sql = "select a.*, b.* from table1 a inner join table2 b on a.col1=b.htno WHERE a.col1 like '$name'";
$sql = "select a.*, b.* from table1 a join table2 b on a.col1=b.htno WHERE a.col1 like $name";
Can someone help me with this? Thank you!
TABLE 1
TABLE2
Join is your friend:
http://www.w3schools.com/sql/sql_join.asp
Combine rows from two or more tables, based on a common field between them.
SELECT * FROM TABLE1 INNER JOIN TABLE2 ON TABLE1.col1=TABLE2.htnon
WHERE TABLE1.col1 = "07731A0328"
The query will be
SELECT * FROM Table1,Table2
WHERE Table1.col1=Table2.htnon AND Table1.col1 = "07731A0328"
I am trying to get a query using fields from 2 tables.
I need to query Table1 but only Table2 has the variable venue_location that I need to query.
Basically I need to count all records on Table1 where Table1.venue_location = $MyVariable.
Here is what I've put together but I believe I need to use Joins for this?
Table1
- venue_id
Table2
- venue_id,
- venue_location
SELECT * FROM Table1 WHERE table1.venue_id = table2.venue_id and table2.location = '$MyVariable'
How can I do a query for this?
Use the power of join table
SELECT * FROM Table1
JOIN Table2 USING(venue_id)
WHERE table2.location = '$MyVariable'
You can get back the count of rows with mysqli_num_rows() in PHP, or change le select by SELECT COUNT(*) AS nbRow FROM ... and check of value in nbRow column
You can join two tables on venue_id and then group it by venue_id where location is your $MyVariable.
Your final query will look like:
SELECT count(table2.venue_id)
FROM Table1
JOIN Table2 ON table1.venue_id = table2.venue_id
WHERE table2.location = '$MyVariable'
GROUP BY table2.venue_id
try this
SELECT Table1.venue_id, Table2.venue_location FROM Table1 INNER JOIN Table2
ON Table1.venue_id='$MyVariable';
Hi I do have an old table and new table with same index/data. TABLE1 and TABLE2
but TABLE1 has got more data than TABLE2. this was maintained by some one and I dont know how this happened. so my question is how do I compare these two table and find which data is TABLE2 missing?? there is almost 200000 datas there so manually doing is not possible...
in PHP:
http://us.php.net/manual/en/function.array-diff.php
in SQL:
SELECT * FROM TABLE1 WHERE id {NOT} IN ( SELECT id FROM TABLE2 )
depending on criteria of comparison
Solution without nested query:
select TABLE1.id from TABLE1 left join TABLE2 on TABLE1.id = TABLE2.id where TABLE2.id is null
Did you mean something like this:
SELECT * FROM TABLE1 t1 WHERE NOT EXISTS(SELECT * FROM TABLE2 WHERE t1.id == t2.id)
By same index I am hoping you mean they share a primary key?
SELECT * FROM TABLE1 WHERE username NOT IN (SELECT username FROM TABLE2)