Select fields from multiple tables

Hi,

I want to select some thing in this table and something else in that table, in one query.

How can I do that, what the query would be like ?

I’m assuming your tables are related in some way, e.g.

table: authors
fields: id, firstName, lastName

table: books
fields: id, title, authorId

Then to return the titles and author names for all books:

SELECT b.title, a.firstName, a.lastName
FROM authors a, books b
WHERE b.authorId = a.id

You can also do this with JOIN, but the above is perhaps more obvious.

Brilliant.

I have looked over the internet for quite an example, I found something simmilar to yours, but could not comprehend the a. b. thing and how the whole thing connects each other.

Through your example above, I am clear now, thank you very much.

No problem :slight_smile:

what if 2 tables have no relating ?

It depends on what you’re trying to do with your query… can you give any more details of the tables & fields?

Like flashparry said, combining two tables together in a query is called a join, and there are several types:
http://dev.mysql.com/doc/refman/5.1/en/join.html

If there’s no relation between the two tables you want to join, the result will be that all rows of the first table will be combined with all the rows of the second query.

table1 : listing
fields: id, title, price, detail, category

table2: category_dictionary
fields: abbreviation, meaning

$query1 = “SELECT
id
, title
, price
, detail
, category
FROM listing
WHERE id = $number”;

$query2 = “SELECT meaning
FROM category_dictionary
WHERE category = abbreviation”;

I want to combine two queries into one

You can’t. Not only do the 2 tables have no relation, but the queries themselves don’t have a relation either.

It’s hard to get the feel of your table structure but I would guess that this is what you are looking for:

SELECT id, title, price, detail, category, meaning
FROM listing l LEFT JOIN  category_dictionary c ON l.category = c.abbreviation