I have problems with a query in mysql help!

0

Hi, I'm doing a library system programmed in php and within that system I have a search engine to view the books. The program already does a basic search and shows results but some fields no longer correspond, the problem or rather the question is how can I make an example query:

  • DB: mydb
  • Table1: title, date, edition, etc.
  • Table2: career
  • Table3: category
  • Table4. editorial.

as I do so that when entering a career title category, editorial,

  • Example1: search by: engineering, and show all the books corresponding to this search that was entered,

  • Example2: Search by: Title, and all the books are shown by title and with the career to which they belong, and thus the same with the category and editorial tables, with table 1 I have no problem but with the others if I do help please !!!

asked by Kevin Duarte 17.03.2017 в 04:51
source

1 answer

1

This design could solve your problem. Since you do not give more details I have considered that all the tables are related to the table books.

To understand the design: all the data are kept separate, each one in your table , and they are linked through an associative table by the respective ids, that allows having in the associative table relations of one to several for example, the case that the same book can belong to several categories or to several publishers, or to several careers, etc. This will also prevent the existence of redundant (repeated) data in each of the tables.

In the example there are two query models, one general and one per career, you can filter by whatever you want, changing the WHERE that goes after the JOIN :

Note: The model shows more or less the structure of the tables, if you decide you add the missing columns, the primary keys, the indexes, etc. to each one. As for the associative table it would be good to establish an index of uniqueness through which controls that do not repeat more than one book with the same book id, category, editorial, career in a single row ...

SQL Fiddle

MySQL 5.6 Schema Setup :

CREATE TABLE libro
    ('libro_id' int, 'libro_titulo' varchar(70) )
;

INSERT INTO libro
    ('libro_id', 'libro_titulo')
VALUES
    (1, 'Tras las huellas de José'),
    (2, '¿Por qué no construimos puentes sobre ríos?'),
    (3, 'Menón')
;

CREATE TABLE carrera
    ('carrera_id' int, 'carrera_nom' varchar(70) )
;

INSERT INTO carrera
    ('carrera_id', 'carrera_nom'  )
VALUES 
    (1, "Filosofía"),
    (2, "Ingeniería")
;



CREATE TABLE categoria
    ('categoria_id' int, 'categoria_nom' varchar(70) )
;

INSERT INTO categoria
    ('categoria_id',  'categoria_nom' )
VALUES
    (1, "Categoria A"),
    (2, "Categoría B")
;


CREATE TABLE editorial
    ('editorial_id' int, 'editorial_nom' varchar(70) )
;

INSERT INTO editorial
    ('editorial_id', 'editorial_nom' )

VALUES
    (1, "Ingenieros Asoc"),
    (2, "Kerygma")
;    


CREATE TABLE asociativa
    ('id' int, 'libro_id' int, 'carrera_id' int, 
     'categoria_id' int, 'editorial_id' int )
;

INSERT INTO asociativa
    ('id', 'libro_id', 'carrera_id', 
     'categoria_id', 'editorial_id' )
VALUES
    (1, 1, 2, 1, 2),
    (2, 2, 1, 2, 1)
;  

Query 1 :

#Todos los libros
SELECT
  li.libro_titulo, ca.carrera_nom, ct.categoria_nom, ed.editorial_nom
FROM
  asociativa ta 

  LEFT JOIN libro li ON ta.libro_id=li.libro_id
  LEFT JOIN carrera ca ON ta.carrera_id=ca.carrera_id
  LEFT JOIN categoria ct ON ta.categoria_id=ct.categoria_id
  LEFT JOIN editorial ed ON ta.editorial_id=ed.editorial_id

Results :

|                                libro_titulo | carrera_nom | categoria_nom |   editorial_nom |
|---------------------------------------------|-------------|---------------|-----------------|
| ¿Por qué no construimos puentes sobre ríos? |   Filosofía |   Categoría B | Ingenieros Asoc |
|                    Tras las huellas de José |  Ingeniería |   Categoria A |         Kerygma |

Query 2 :

#Por Carrera
SELECT
  li.libro_titulo, ca.carrera_nom, ct.categoria_nom, ed.editorial_nom
FROM
  asociativa ta 

  LEFT JOIN libro li ON ta.libro_id=li.libro_id
  LEFT JOIN carrera ca ON ta.carrera_id=ca.carrera_id
  LEFT JOIN categoria ct ON ta.categoria_id=ct.categoria_id
  LEFT JOIN editorial ed ON ta.editorial_id=ed.editorial_id

  WHERE ca.carrera_id=1

Results :

|                                libro_titulo | carrera_nom | categoria_nom |   editorial_nom |
|---------------------------------------------|-------------|---------------|-----------------|
| ¿Por qué no construimos puentes sobre ríos? |   Filosofía |   Categoría B | Ingenieros Asoc |
    
answered by 17.03.2017 / 05:32
source