I need help with a sql statement

2

You see, I am carrying out a project little by little and I have found a doubt that requires knowledge of sql and the truth is that I know the basics ...

I need an ajax request to make me a select that returns the fields of 2 tables, but one of those tables. I need a single field ...

I put the structure

Table Artist

id | name | user | password | mail | phone | genre | locality | description | web | photo

Event Table

id artist_id | title | date | description | date_event

Both tables relate by artist id - event id_artista

So far I've tried sentences like ...

SELECT * FROM artista a, evento e WHERE a.nombre =  "paco" AND e.id =26
Ésta no me valía, ya que no necesito uno en concreto, sino todos los artistas que han creado eventos... y por ello requiero de su nombre, que está en la tabla artista.

SELECT * FROM artista a, evento e;
ésta me devuelve todos los campos de ambas tablas, y yo solo necesito el nombre...

I want the entire event table ... and from the Artist table only the name ... how can I do it?

I appreciate the inconvenience. Thanks

    
asked by DRapLow 29.04.2018 в 14:34
source

2 answers

1

You need to join between the two tables to have all their attributes

SELECT a.nombre,  e.*
FROM artista a inner join evento e on a.id_artista=e.id_artista
    
answered by 29.04.2018 / 15:07
source
3

You need to use a JOIN clause. I leave you a link with examples to help you better understand the resource.

Your case is very simple and with a natural join it would be enough, although you do not offer much information (such as the database manager, the query that does not work for you, etc.). The query may be something similar to the following:

SQL> SELECT e.id, e.titulo, e.fecha, e.descripcion, e.fecha_evento, a.nombre
  2  FROM evento e
  3  NATURAL JOIN artista a;

A rough way would be something like that. With more data or your attempts, I could make it more explicit. But it seems a very simple query. If you do not get it, improve your question and I'll give you a hand. In this way, you can select the fields you need expressly. Also, since the only common field is the id, the join will be made based on it, so you do not have to specify it.

I also attach a very descriptive image of the types of joins that will help you:

    
answered by 29.04.2018 в 15:07