playdewu playdewu - 7 months ago 14
SQL Question

Select rows from table1 and (row=row_from_table1) from table2

I have 2 tables. From first I want to select all rows and from table2 row=row_from_table1.

What i mean:

SELECT SUM(amount) AS value_sum, itemid_table1, itemname, date, cost FROM bets


and

SELECT participate FROM items WHERE itemid='itemid_table1'


Is possible to do it in one query with returning value_sum, itemid_table1, itemname, date, cost and participate?

A J A J
Answer

I would suggest you to go through this link.

http://dev.mysql.com/doc/refman/5.7/en/join.html

In your case, you can try.

SELECT SUM(b.amount) AS value_sum, b.itemid_table1, b.itemname, b.date, b.cost, i.participate
FROM bets b INNER JOIN items i ON b.itemid=i.itemid_table1
WHERE b.itemid='DESIRED_VALUE_HERE'