mysql varius sentences





  1. I see all foreign keys to a table or column: SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_NAME = '<table>'
  2. Distinc values in 2 columns from 2 tables:SELECT
     SELECT DISTINCT t1.`ID_POSTULANTE` AS T1_ID_POSTULANTE FROM
      `tb_postulante` t1 INNER JOIN `aulaasignada` as t2
       ON t1.`ID_POSTULANTE` != t2.`ID_POSTULANTE`
       GROUP BY T1_ID_POSTULANTE
  3. values duplicate for one column: SELECT certificado_id_correlativo FROM t_certificado_digital group by certificado_id_correlativo having count(*) >= 2
  4. update from select: update tabla1 inner join tabla2 on tabla1.id_tabla2 = tabla2.id set tabla1.campo=tabla2.language
  5. update year sales: update sales set created_at=DATE_FORMAT(created_at, '2020-%m-%d') where YEAR(created_at) < 2020 ;
  6. update month sales: update sales set created_at=DATE_FORMAT(created_at, '%y-10-%d') where MONTH(created_at) < 10 ;
  7. update day sales: update sales set created_at=DATE_FORMAT(created_at, '%y-%m-25') where DAY(created_at) < 10 and MONTH(created_at) = 10 ;
  8. update month invertval sales: update sales set created_at=date_add(created_at, interval 1 month);
  9. insert select:
    1. INSERT INTO generic_stage.entity_items (
          client_id,
          item_type_id,
          item_id,
          action_type,
          action_result,
          marketing_flag,
          creation_timestamp
      )
      SELECT
          '300001' AS client_id,
          8 AS item_type_id,
          cg.id AS item_id,
          1 AS action_type,
          1 AS action_result,
          0 AS marketing_flag,
          NOW() AS creation_timestamp -- use NOW() for current timestamp
      FROM generic_source.item_source cg
      WHERE source_provider_id = 7;











source:

0 comentarios :