Most voted "sql" questions
Structured Query Language (SQL) is a language to query databases. Questions should include examples of table code and structure. This tag refers to the default language, not to be used for questions about the implementation of specific DBMS (such as Mysql, Postgresql, Oracle, MS SQL Server). In this case use the specific DBMS tag. Answers to questions marked with ANSI SQL should use SQL as much as possible.
Learn more…6,771 questions
Sort by count of
-
0
votes4
answers44
viewsCopy multiple records to the same table with change of values
I have the following query: INSERT INTO tags group_tags, title, active SELECT group_tags, title, active FROM tags WHERE group_tags = 2; First How to run this query for each table record that…
-
0
votes1
answer130
viewsPercentage in SQL
I have the following consultation code: SELECT AC.DT_AGENDA AS DATA, ME.DS_MULTI_EMPRESA AS EMPRESA, AC.CD_UNIDADE_ATENDIMENTO AS CD_UNID_ATENDIMENTO, UA.DS_UNIDADE_ATENDIMENTO AS…
-
0
votes1
answer48
viewsWhy does the Crosstab function not work in Windows? Using Postgresql
I built a query that has a Crosstab, in the database manager works perfectly and gives no error, but when I run this query inside my project that is in Laravel, it explodes an error saying that the…
-
0
votes1
answer51
viewsHow do I bring the same numbers from other tables?
Good morning, I’d like that help. I have a table of "INSCRIPTION" where it has 2 foreign keys (CAND_SQ and CANDPL_SQ). Both foreign keys have a CPF field. I would like to know how I bring the…
-
0
votes1
answer28
viewsSQL performing wrong query
I am trying to identify which products in stock have reached the minimum capacity or are below it. I tried it this way: SELECT * FROM produtos WHERE quantidade_minima <= quantidade And I get the…
-
0
votes0
answers46
viewsUPDATE between 2 SQL tables giving error
I am trying to perform an UPDATE and it brings me the following error: "Message 157, Level 15, Status 1, Line 40 - An Aggregate may not appear in the set list of an UPDATE statement." Follow the…
-
0
votes0
answers18
viewsMYSQL - Is there an alternative to LIMIT in the Queries?
I’m using MYSQL. I have the following table: create table visita ( id int, titulo_socio varchar(10), data_visita date, cidade_visitada varchar(50), status_visitacao varchar(50) ); A data example for…
-
0
votes1
answer70
viewsHow do I subtract and display the result in the MYSQL query
I have 3 tables Vehicles idVeiculo | Placa | Prefixo oleomotor idMotor | idVeiculo | datatroca | kmtroca | horimetroca | proximatroca |idMecanico I already have this query below that shows the…
-
0
votes1
answer221
viewsHow to Subtract MYSQL
How to do the correct subtraction of these two columns in the SQL script? SELECT DISTINCT b.prefixo as idVeiculo, max(datatroca) as datatroca, max(kmtroca) as kmtroca, max(horimetroca) as…
-
0
votes0
answers31
viewsLeft Join in Sql Server
I have a question, there is some difference in executing the querys below? SELECT C.NOMECARGO, F.NOMEFUNCIONARIO FROM CARGO AS C LEFT JOIN FUNCIONARIO AS F ON C.IDCARGO = F.IDCARGO SELECT…
-
0
votes1
answer168
viewsIs there an equivalent to SQLSERVER’s @ROWCOUNT in Postgres?
I am migrating the Database of some applications, from Sqlserver to Postgres and several scripts use @@rowcount. There is a Postgres equivalent to @rowcount?
-
0
votes0
answers42
viewsJOIN WITH 3 TABLES
I have 3 tables which I want to bring only data from table 1, if you do not have in table 1 to bring data from table 3 , if you have data in the 3 tables bring only from table 3. My problem is that…
-
0
votes0
answers24
viewsCreate PHP encrypted file download link
I have a site where users can download the files. These files are with the path in an sql table and have an access key and the request id. Hj file download is direct at url, ex: <a…
-
0
votes1
answer49
viewsColumn that receives a result if the condition is true
SELECT DISTINCT P.CD_PACIENTE , P.NM_PACIENTE , DECODE (P.TP_SEXO, 'M', 'MASCULINO', 'F', 'FEMININO', 'I', 'INDEFINIDO') SEXO , TRUNC(P.DT_NASCIMENTO) DT_NASCIMENTO , (SELECT FN_IDADE…
-
0
votes1
answer22
viewsReturn values if any record does not exist
I have the tables: tabelaUm [id] [nome] 1 nome1 2 nome2 tabelaDois [id] [idTabelaUm] [nome] 1 1 nome1 1 1 nome2 1 1 nome3 1 2 nome4 1 2 nome5 1 2 nome6 My query: SELECT t1.nome AS n1, t2.nome AS n2…
-
0
votes1
answer100
views -
0
votes3
answers34
viewsHow do I filter a time coming through a select
$query = "SELECT horario FROM tempo WHERE teste = $teste"; Way it returns: 2019-07-26 19:24:17 Way I want it to return:19:24:17
-
0
votes2
answers55
viewsGive an input in a table with information from two tables
I would like to know how to insert in a table ids of two other tables. For example, there are 5 tables one is student, which has the following information: +--------+------------+ | ID_ALU | nome |…
-
0
votes0
answers34
viewsA query with related answers
Great pleasure to be here. I would like some information. What would be the most correct form or name to make a tool like this: I would like to make a filter in the database using check boxes on the…
-
0
votes2
answers136
viewsReplace a fixed CASE value with a SELECT and JOIN?
SELECT DISTINCT P.CD_PACIENTE , P.NM_PACIENTE , DECODE (P.TP_SEXO, 'M', 'MASCULINO', 'F', 'FEMININO', 'I', 'INDEFINIDO') SEXO , TRUNC(P.DT_NASCIMENTO) DT_NASCIMENTO , (SELECT FN_IDADE…
-
0
votes1
answer961
viewsCombobox receive data in the second field (list menu)
Question: I have 2 Select Menu and I need the second select to change with data group according to the choice of the first one. For example, make of cars and models. In the first select the brands…
-
0
votes2
answers817
viewsUpdate to numbering in column order
Good afternoon! I am with a situation in my service, I am beginner in sql server and would like a help or tip if possible. I have some records (54) of a table that need to be changed its code…
-
0
votes1
answer2565
viewsHow to know the date of each record that was entered in the database?
Good afternoon! I have a small database that stores the records of several people. Unfortunately no behavior was created to record the date and time of the record saved in the database. You can find…
-
0
votes1
answer41
viewsList column of one table with all records of another
I have 2 tables, posts and posts_images, the table post owns the fields id, title, Category, and images, and the table posts_images possesses: post_id, img_url etc. I would like to relate the field…
-
0
votes1
answer187
viewsSQL server error to create table
I tried to create several tables in SQL but 2 of them gave the same error: "Msg 1767, Level 16, State 0, Line 72 Foreign key 'Fk__movimento__codem__3118447e' References invalid table "emprestimo".…
-
0
votes1
answer78
viewsHow to configure Mysql for multiple connections?
Hello, I have a client with 3 points of sale, 1 office and 1 server. Such a server is Mysql. When a certain export routine is performed in the office, a query is executed that takes a while to…
-
0
votes1
answer72
viewsSQL - Decimal out of range
I have a table that represents products in stock, it has a quantity field DECIMAL(5,3), i understood that are 5 digits before the comma and 3 after the comma, up to 2 decimal places before the comma…
-
0
votes0
answers88
viewsSql Server Database Modeling
Good morning friends, I have a project that has basically two types of products, the simple products, such as market items, rice kg, beans 1 kg , etc.. and items composed as basic baskets , formed…
-
0
votes2
answers42
viewshow to show the result of an sql in different <Section> files?
How do I make projects belonging to the same department stay in the same department? $sql = "SELECT d.nome, p.titulo FROM projeto p, departamento d WHERE p.dpto_id=d.id ORDER BY d.nome, p.titulo";…
-
0
votes1
answer89
viewsSubquery with mysql with two tables
Guys I’m trying to do a subquery in Where as a condition to list the average hours worked greater than 20 and the title of the codProj with 2 tables Following the code: SELECT…
-
0
votes0
answers98
viewsHow to RETURN an UPDATE from SELECT in Postgree within a Function?
I have a "FUNCTION RETURNS TABLE()" in Postgree, where I select from select, but I need to apply an update from select, but I cannot return to the table. I tried it this way and I didn’t succeed:…
-
0
votes1
answer33
viewsSQL: Sort by columns from another table
I have a user tabala, and another with the respective scores, I need to select from the scoreboard only users who live in a certain country. The country is defended in the users table, each table…
-
0
votes2
answers78
viewsMake teams combination in SQL without repetition
Good morning, I have a database here with the tables below, and I wanted to generate a table with the combination of the registered teams Time ------ Time A Time B Time C Time D The final table…
-
0
votes1
answer689
viewsLong to Varchar data conversion
I am trying to convert data from a Long field to Varchar, but when I do the procedure, it is returning the following error: ORA-01422: Exact extraction returns more than the requested number of…
-
0
votes3
answers196
viewsHow to select to find a record between two values?
I have a table with the following values # valor_min vlr_max juros 1 0 1999 0 2 2000 2999 1.2 3 3000 3999 1.3 4 4000 999999 1.4 I need to consult this table with the value ex.: 2500,00 find the…
-
0
votes1
answer101
viewsSelect to join two Father and Son tables one below the other
I would like to know how to get the result according to the image, I have a column with id that links the father and son records, I know that the join joins the tables side by side, however, needed…
-
0
votes1
answer133
viewsCatch record with JDBC SQL specification
Hello, good afternoon, sir. I’m having to develop a Java system with Mysql. It’s simple, just the part of the backend, but I’m inciante and had not run querys with java before hehe. The system is…
-
0
votes1
answer109
viewsHelp with DBMS_STATS.GATHER_TABLE_STATS - Oracle for DB2
I am passing an Oracle protocol for DB2 and I am not able to run on DBMS_STATS account.GATHER_TABLE_STATS : DBMS_STATS.GATHER_TABLE_STATS ('DMTLDBR','TB_FATO_OBJETIVO',PART_MES_ANT,10, TRUE,'FOR ALL…
-
0
votes0
answers21
viewsAlternative method for OR condition
I would like to know if you have any alternative means to improve the performance of a select without using the expression OR. Running only with an equal operator I get the result in 30min but…
-
0
votes1
answer82
viewsSELECT and INSERT - Inserting only the first data
I wanted to save the two data (from sector and from sector) but he is saving only the first in the two Insertion script (html): <div class="form-group"> <label for="de_setor"…
-
0
votes0
answers152
viewsTrunc date oracle, Tuning SQL
Is there any way to optimize query sql, where it is necessary to truncate two dates of a column? Select * from work_table a where trunc(a.datetime) = trunc(a.datetime);…
-
0
votes1
answer184
viewsSequential update SQL
I have a table SGI01A in my database, where a column of records follows the following logic: At some point, this ordination went out of order and began to count in a "wrong" way. What was to be…
-
0
votes1
answer289
viewsTrigger with sum between dates
Good evening, I need a help I’m not getting, I have two tables in my db. 1 - Portfolio table composed of: CREATE TABLE `carteira` ( `id` int(11) NOT NULL, `datat` datetime DEFAULT NULL, `vl_cliente`…
-
0
votes1
answer223
viewsDoubt in SQL query with Group by and Order by
Good afternoon Galera!!! I have an sql query that is simple, but I’m having a hard time doing an order by in two fields at once. Such consultation is for me to assemble a report, but before mounting…
-
0
votes2
answers118
viewsLast Registration by date
How do I return the latest update to a record example the record may have several status in one day but need to return the latest…
-
0
votes1
answer436
viewsPLSQL shows error: PLS-00487: Invalid reference to Variabel
I have a PLSQL code: in which I receive two values one numerical and another varchar. Code: create or replace FUNCTION hospitalTeste123(MatriculaMedico in number, nomeSchema in varchar2 ) return…
-
0
votes1
answer456
viewsApply a regex to the result of a select
I am making a query to get all the projects that are being executed: SELECT p.id FROM projetos p WHERE p.status LIKE 'Execução' An example of the result: COD00012347 COD00012348 COD01212349…
-
0
votes2
answers177
viewsRun an SQL script in transaction without it blocking other scripts accessing the same table
Can anyone tell me if there is a way I can execute an SQL Server script in transaction without it blocking other scripts that try to access the same table? I created a program in c# that accesses my…
-
0
votes1
answer152
viewsPostgresql result filtering from lower one column value
It is a matter of logic and knowledge of Postgresql resources. I have the following result of a query: I need to do a filtering, grouping by [hour] so that only the nearest [real time] remains and,…
-
0
votes1
answer1049
viewsHow to make a "scan" using SQL?
I have the following structure in my database: Table tgffin with the columns: CODNAT, AD_CODNAT_OLD Table tgfnat with the columns: CODNAT, AD_NAT_OLD I want to reclaim the value of tgfnat.CODNAT…