i have two similar tables with the same fields. One is named "input" and the other "product" Both have the same fields:
id
name
qty
unit
price
pic(fk - both reference the same picture table).
My first question is: How can I write a query to effectively return every item's details?
I use JSON to export this data
$json_data.='"rec'.$i.'":{"id":"'.$id.'", "name":"'.$name.'", "price":"'.$price.'", "qty":"'.$qty.'", "units":"'.$unit.'", "pic":"'.$pic.'"},';`.
My second question is: How can i effectively reference all the fields from both tables?
NB: I don't want to put the two categories in the same table, say, item, with an additional field of category.
Just to rectify this: the join condition is (input.pic=pic.id and product.pic=pic.id)
If the tables have the same fields you can do a union query.
SELECT * FROM table1
UNION
SELECT * FROM table2
if they are similar as you say then you just have to select the fields that they have in common into both
SELECT "table1" as source_table,id,name,qty,unit,price from table1
UNION
select "table2" as source_table,id,name,qty,unit,price from table2
note I added a source table so you could backtrack where you got it from .... cheers
use full outer in mysql this is not possible so use/take help of union to get all the rows
SELECT * FROM input
LEFT JOIN product ON input.id = product.id
UNION
SELECT * FROM input
RIGHT JOIN product ON input.id = product.id
The problem with that scenario is the word "efficient". The pic - input - leftjoin is slower than an inner join, and the sum of left join input and left join output is slower than a simple inner join.
select pic.id as pic_id, pic.data, input.id as input_id, output.id as output_id, input....
from pic
left join input on input.pic_id = pic.id
left join output on output.pic_id = pic.id
or
select pic.id as pic_id, pic.data, "input" as type, input.id as id, input....
from pic
inner join input on input.pic_id = pic.id
union
select pic.id as pic_id, pic.data, "output" as type, output.id as id, output....
from pic
inner join output on output.pic_id = pic.id
The union is faster, because of using two inner joins.
Related
I have a little problem with INNER JOIN in mysql query. I have two tables the first is 'kontrola' and 2nd is 'naruszenia' In 'kontrola' table I have rows:
id
podmiot
miasto
wszczeto
zakonczono
naruszenie_id (Foreign KEY on 'naruszenia' table)
In my naruszenia table I have:
id
naruszenia
Now I want to display using INNER JOIN the naruszenie from 'naruszenia' table.
I've create somethin linke this:
$listakontroli = $connecting->query("SELECT * FROM kontrola INNER JOIN naruszenia ON
kontrola.naruszenie_id=naruszenia.id");
But the result is that when I want to display records in table I have changed the ID of first table(kontrola) and the naruszenia_id still showing id from naruszenia table. How to change it to display properly the word not id.
You could use explicit column name and refer to both the table (in this case using k an n) eg:
$listakontroli = $connecting->query("SELECT k.id
, k.podmiot
, k.miasto
, k.wszczeto7
, k.zakonczono
, n.naruszenia
FROM kontrola k
INNER JOIN naruszenia n ON k.naruszenie_id=n.id");
You need to use LEFT OUTER JOIN or separate the ID from the two tables. e.g.
$listakontroli = $connecting->query("SELECT kontrola.id as kid, naruszenia.id as nid, podmiot, miasto, etc* FROM kontrola INNER JOIN naruszenia ON kontrola.naruszenie_id=naruszenia.id");
This way you can properly distinguish the displayed IDs
I have 2 tables MOVIES and SHOWS.
MOVIES tables contains:
id, name, image, description.
SHOWS table contains:
id, movieid, description.
I'm executing mysql statement to retrieve records from SHOWS table, i'm getting all the records normally. Again I'm executing another mysql statement to get image from MOVIES table based on movies table id which i'm getting from first query.
Is there any simple way to retrieve all the records from SHOWS table along with movie image?
These are my queries:
$qry1 = mysql_query("SELECT * FROM shows WHERE id='1'");
$res = mysql_fetch_array($qry1);
$movieid = $res['movieid'];
$qry2 = mysql_query("SELECT image FROM movies WHERE id='$movieid'");
SELECT t1.id, t1.movieid, t1.description, t2.image FROM SHOWS as t1
INNER JOIN
MOVIES as t2 ON t1.id = t2.id
Some sql join docs
or you can try this, i was not sure witch id is from where:
SELECT id, movieid, description, image FROM SHOWS
INNER JOIN MOVIES
ON id = movieid
Some foreign key docs
Here I am writing with out join you can all so use nested select queries
select movies.image from movies where movies.id in(Select shows.movieid form shows where shows.id= 1);
You can retrieve data from multiple tables from one server at the same time. There is a lot of ways to achieve this operation which is called join. One of the possibility would be the LEFT JOIN like this:
SELECT t1.field1, t1.field2,..., t2.field1, t2.field2, ..., t2.fieldn
FROM table1 AS t2
LEFT JOIN talble2 AS t2 ON t2.some_field = t1.anothed_filed
WHERE some_condition
In you case:
SELECT s.*, m.image
FROM SHOWS AS s
LEFT JOIN movies AS m ON s.movieid = m.id
WHERE some_condition
for more info see the documentation on https://dev.mysql.com/doc/refman/5.7/en/join.html
I have two tables. Both have fields called "ID".
Table 1 has "ID", "Title", "Shift"
Table 2 has "ID", "Table1ID", "Details"
I would like to query Table 2 and retrieve all it's details based on an "ID" value but also get the values from Table 1 that relate to the "Table1ID" value.
I've tried this...
SELECT * FROM Table2 a, Table1 b WHERE a.TableID = b.ID
This works but only retrieves one tables "ID" field.
I've been playing about with UNION ALL but can't get it to work.
Any ideas?
Thanks
Yes, you can add an alias:
SELECT a.ID AID, a.Title, a.Shift, b.ID BID, b.TableID, b.Details
FROM Table2 a, Table1 b
WHERE a.TableID = b.ID
The above will return ID from table A and B as AID and BID in the result.
Is there a way to get the SELECT to get all the fields (without
explicitly writing them) and still alias a specific field?
yes its possible but is considered harmful
This is how you would do id
SELECT Table1.*,Table2.* from table1 inner join Table2 on Table1.ID = Table2.Table1ID;
Why I'm saying its harmful see here : Why is SELECT * considered harmful?
Proper select should be :
SELECT Table1.ID,Table1.Title,Table1.Shift,Table2.ID.Table2.Table1ID,Table2.Details.* from table1 inner join Table2 on Table1.ID = Table2.Table1ID;
Is it possible to specify the fields that you want in the left joined table
i.e.
SELECT * FROM students
LEFT OUTER JOIN classes_enrolled (can i specify fields here)
ON students.student_id = classes_enrolled.student_id
I only wanted to get the field subject from the left joined table classes_enrolled rather than have * all the fields in the matched rows being appended
Why not try something like
SELECT students.* ,
classes_enrolled.subject
FROM students LEFT OUTER JOIN
classes_enrolled ON students.student_id = classes_enrolled.student_id
It is actually considered good practice to specify the field names, rather than using SELECT *
you can choose any of these:
using subquery to select on specified columns,
SELECT *
FROM students
LEFT OUTER JOIN
(
SELECT student_id, subject
FROM classes_enrolled
) b ON students.student_id = b.student_id
or the one that I prefer -- to manually select these columns,
SELECT students.*,
classes_enrolled.subject
FROM students
LEFT OUTER JOIN classes_enrolled
ON students.student_id = classes_enrolled.student_id
Try below
SELECT , classes_enrolled.subject, * FROM students,
LEFT OUTER JOIN classes_enrolled
ON students.student_id = classes_enrolled.student_id
I'm having trouble building a query. I can do what I want in 3 different queries.
SELECT id FROM table1 WHERE url LIKE '%/$downloadfile'
put that in $url_id
SELECT item_id FROM table2 WHERE rel_id = '$url_id'"
put that in $item_id
SELECT rel_id FROM table2 WHERE rel_id = '$item_id' AND field_id = '42'"
put that in $user_id
But from reading examples on joins and inner joins I think there's a more elegant way. I cant wrap my brain around writing a better query (but would like to) I can describe how it should go:
table1
fields: id, url
table2
fields item_id, rel_id, field_id
I know the last part of table1.url (LIKE '%/$filename') with that I select table1.id.
table1.id is equal to one entry in table2.rel_id. So get that and select the table2.item_id.
In table2 there is another entry which has the same table2.item_id and it will have a table2.field_id = '42'
And finally the value I need is the table2.rel_id where the table2.field_id was 42.
I will fetch that value and put it in $user_id
Can this be done with one query using joins/inner joins?
SELECT url, second.rel_id AS user_id
FROM table1
INNER JOIN table2 AS first
ON table1.id=first.rel_id
INNER JOIN table2 AS second
ON first.item_id=second.rel_id
WHERE second.field_id='42'
AND table1.url LIKE '%/:downloadfile'
Sure, should be something like:
SELECT t2_second.rel_id
FROM table1 t1
INNER JOIN table2 t2_first ON t1.id = t2_first.rel_id
INNER JOIN table2 t2_second ON t2_second.rel_id = t1_first.item_it
WHERE
t1.url = '%/:filename' AND
t2_second.field_id = 42
You could even throw in a distinct so that each t2_second.rel_id is only returned once:
SELECT DISTINCT [...]
You might not need this, however, if t2_second.field_id = 42 will restrict the query to only return one row for each t2_second.rel_id
This the query that works for me, made one small change to Ignacio's answer:
SELECT url, second.rel_id AS user_id
FROM table1
INNER JOIN table2 AS first
ON table1.id=first.rel_id
INNER JOIN table2 AS second
ON first.item_id=second.item_id
WHERE second.field_id='42'
AND table1.url LIKE '%/:downloadfile'