How to show duplicate MySQL and PHP records?

1

How can I get duplicate records of a product? I'm developing a site to sell video games online, I have a search bar to show the titles by entering their SKU, but I want to show them the games that have their duplicate. For example:

===================================================================
|| SEQJ || Nombre_Juego || Categoria_Juego  || Cant_Disp ||  SKU ||
||=================================================================
|| 001  || gears_of_war || accion_disparos  ||    10     || 0123 ||
|| 001  || gears_of_war || accion_disparos  ||    20     || 0124 ||
|| 001  || gears_of_war || accion_disparos  ||    20     || 0125 ||
|| 002  || metal_gear_5 || sigilo_accion_a  ||    20     || 0126 ||
|| 002  || metal_gear_5 || sigilo_accion_a  ||    20     || 0127 ||
|| 002  || metal_gear_5 || sigilo_accion_a  ||    20     || 0128 ||
|| 003  || god_of_war_1 || accion_3_persona ||     5     || 0129 ||
|| 003  || god_of_war_1 || accion_3_persona ||    30     || 0131 ||
|| 003  || god_of_war_1 || accion_3_persona ||    30     || 0132 ||
|| 005  || pro_evo_socc ||     deportes     ||     2     || 0134 ||
==================================================================

What I need is that when searching for the SKU of the product I bring all the products (for example, all gears of war) and not only the last value (in the example of gears_of_war with SKU 0125, but all gears_of_war )

Note: There are repeated, but I need you to bring me those repeated (when it is the case). Greetings.

    
asked by SmithBit 16.01.2017 в 05:21
source

2 answers

2

You could execute a query as follows:

/*
    query para obtener los elementos de tu tabla (no sé el nombre)
    basados en el valor de SEQJ
    el cual lo obtendremos de la misma tabla pero mediante un subquery
*/
SELECT t2.SEQJ,
  t2.Nombre_Juego,
  t2.Categoria_Juego,
  t2.Cant_Disp,
  t2.SKU
FROM tabla t2
WHERE t2.SEQJ = (
    /*
        subquery para obtener el SEQJ que indica los juegos
        colocamos LIMIT 1 para que siempre devuelva 1 resultado
    */
    SELECT t1.SEQJ
    FROM tabla t1
    WHERE t1.SKU = :sku //aquí iría el valor del SKU que buscas
    LIMIT 1)
    
answered by 16.01.2017 в 05:35
0

Assuming that the field SKU does not have repeated values in the table, with a simple join to the same table it can be achieved:

select t2.*
  from vgames t1
  join vgames t2
    on t2.seqj = t1.seqj
 where t1.sku = '0125'
 order by t2.sku

Note that I put a ORDER BY so that the results come out in order of SKU . You can modify it to get the records in the order you want. Just make sure to use a field of t2 , not t1 . Or, if the order does not matter to you, you can simply remove the clause ORDER BY .

    
answered by 16.01.2017 в 10:18