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
votes0
answers26
viewsLimit month to export report
Good morning, everyone, I have a system, and it exports the company’s YEAR report. I wanted to limit this report to 18 months. That is, the user can make a report of no more than 18 months, besides,…
-
0
votes3
answers44
viewsReturn larger field to ID
Good morning, I need some guidance regarding the following situation: In this table I need to return only the MAX(nutab) for each codprod and control. I tried using the MAX, but so only returned…
-
0
votes1
answer86
viewsSub query in a single SQL field
I need to create a code where I update a field always adding a value to what already exists. Example table assuming this population is already CAMPOS: ID | EMAIL | QUEMCURTIU 1 |[email protected]| 3, 46, 81,…
-
0
votes1
answer174
viewsHow to translate this query to Codeigniter query Builder?
I’m having difficulties to assemble this query in Codeigniter, someone can give a little help? select venda_itens.secao, venda_itens.secao_nome, sum(venda_itens.qtd_itens_finalizada) as qtd_itens,…
-
0
votes2
answers228
viewsCan I create a Foreign Key between 3 tables?
I have 3 main tables in my database: Actors, Moviesdata and Seriesdata. Each film and each series has its own actors, but series and films can share the same actor. How do I create a key Foreign…
sqlasked 5 years, 6 months ago Diogo Teixeira 79 -
0
votes0
answers25
viewsDoes the following equation respect the syntax of Relational Algebra?
π Nome.Empregado (σ Empregado.IdentEmp = TrabalhoNo.IdentEmp ^ TrabalhaNo.ProjNum = Projeto.ProjNum ^ Nome = ‘Banco de Dados’ ((Empregado x TrabalhoNo) x Projeto)) More specifically in the part I…
-
0
votes0
answers264
viewsSQL, List number numbers that are not in the table sequence
How could I list numbers that are missing to complete the sequence of numbers given column of my table? see the link to view my test base: http://sqlfiddle.com/#! 9/d124553/1 See below my select to…
-
0
votes0
answers38
viewsCheck items in history N:N
Currently I have a basic asset control system with the following structure: As you can see, I have a Many-To-Many table, which is called TB_HISTORICO_ATIVO. It has all the history the asset has…
sqlasked 6 years, 7 months ago Phoenix209 1 -
0
votes1
answer158
viewsORDER BY RAND() in 2 columns
Well earlier I posted a problem about the elimination of 1 select which was solved but now I appeared another problem . My idea is to create a playlist that prioritizes status 1. Whenever a user…
-
0
votes1
answer89
viewsHow to create a real-time list with PHP and Ajax
I’m creating a recipe site, where the ingredients will be registered individually in a table, referencing the recipe id, example of how the tables are: ptp_receitas id|nome 1 |receita de feijão…
-
0
votes1
answer52
viewsDate Subtraction and return positive SQL value
Good afternoon, I’m trying to build a Query in which I subtract two dates to return the value in days, but I only need the days that returned positive, I ran the Query that I assembled but had no…
-
0
votes1
answer22
viewsPostgresql query bringing more results than expected
Hello, I have a situation in a system where I need to find out which student enrollments are related to a tutor in a class. The tables I have are: matricula containing foreign key with student…
-
0
votes0
answers770
viewsSQLSTATE error[42S22]: Column not found: 1054 Unknown column '' Laravel project, table with two foreign keys
Controller function for project voting has two foreign keys, user_id and projeto_idProjeto, follows function: public function votar($id){ $usuario = Auth::user()->id; $votoss =…
-
0
votes1
answer37
viewsCTE: Union of two tables in which one of them has only one field in common
I need to do the following using a CTE.Imagining two tables with columns: Articles: Reference, Quantity, Price Associated References: Reference, Referral I intend that if there is an associated…
-
0
votes1
answer66
viewsHow to Group the results of a Select into two tables -- SQL
Well I have two tables the Tbinfo and the table Tbprograma CREATE TABLE TBInfo( Ip VARCHAR(20), HostName VARCHAR(50), Mascara VARCHAR(15), Gataway VARCHAR(15), Win VARCHAR(50), Versao VARCHAR(8), Hd…
-
0
votes2
answers282
viewsComparing date and time in different fields
I have a table that contains the following fields type name (nvarchar), Date of type (Date) and time like (Time). I need to do the comparison by period example: 08-02-2019 e 18:30:10 até 30-05-2019…
-
0
votes1
answer53
viewsAdd hours of all employees during the month
I am developing a system in PHP and Mysql where a person does some overtime during the month. pessoa = id - 5 - int /nome - 15 - varchar / boletins = id -5 - int / idpessoa - 5 - int / horaextra -…
-
0
votes1
answer50
viewsSelect to show clients that are not in another table
What is the correct way to present my client table with the following clauses: Be more than 60 days without purchase, not in the Links table or have more than 20 days since the last call. Fields…
-
0
votes2
answers78
viewsSQL + PHP Query: Take only data from the current year from January to Today?
I need the return of data between 01/year to the present day. But I’m still not very familiar with SQL and PHP date(); Here is the excerpt from the code: $startMP = date('Y'); 'BETWEEN '{$startMP}…
-
0
votes1
answer31
viewsReturn last value of each day in mysql
Hello, I have following values in the database: data - valor 2019-06-03 7:00 9 2019-06-03 12:00 21 2019-06-03 20:00 28 2019-06-02 8:00 11 2019-06-02 12:30 21 2019-06-02 19:50 27 2019-06-01 8:30 10…
-
0
votes1
answer58
viewsComment system with foreign key "autor_coment"
Has a comment table with the following attributes: id (int not null), comment (text not null), data_coment (timestamp not null) and autor_coment (int not null). The autor_coment is a FK referencing…
-
0
votes1
answer48
viewsUpdate of an installment form query
Good afternoon, today I had a little doubt. Somewhere in my code I have a query to do an update. The function is as follows: function updateUser(user_id, params) { const query = `UPDATE users SET…
-
0
votes2
answers1317
viewsTurn rows into SQL columns
I have the following Query: SELECT distinct finmovem.num_transacao ,finempe.codigo_orgao ,finempe.cod_reduzido ,finempe.num_empenho ,finempe.data_empenho ,finempe.nome_fornecedor…
-
0
votes1
answer251
viewsTo repeat the value of a field in another field in select SQL Server
I have a table where there are the fields: Personal, Name and Personal (which indicates that she is someone else’s daughter from the table. and if she is the father, that field is null). I need to…
-
0
votes1
answer883
viewsSpecial Characters - SQL
Does anyone know how I can filter only characters like in the example below ? Email Jhó[email protected] Khã[email protected] As seen above it appears ' ' ' and '~' and may also appear '.. ' or ',' and…
-
0
votes1
answer63
viewsA table just to store images or split by types?
I want to register users, where they should put their profile photo and inside the program, store other images, I better make a table only for all photos or split for each type? I saw some places…
-
0
votes3
answers3992
viewsConvert Date to year / month
I was able to convert the date to year / month, but I need the date to go like this: 2019/04, but it’s coming out like this: 2019/4 how can I fix this? SELECT CONVERT(VARCHAR(4), DATEPART…
-
0
votes2
answers92
viewsDelete duplicate records in MYSQL
I am using a command that runs every second in Node.js. It has the function of deleting any record in duplicity for a particular item, which is specified as in the example by AND t1.auction_id =…
-
0
votes1
answer245
viewsInner Join is returning repeated results
I have three tables! One of them is a relation table! Are they: Person: Treating: Relationship Table between Person and Treatment: I would like that when passing the person’s ID, the query returns…
-
0
votes0
answers25
viewsI want to update record of a table through another database with equal columns
I want to update the ordem_subclass column of the Prod_serv Table, and the reference are the codecolumns of the Prod_serv Table and code of the Subclass Table: USE S9_Real UPDATE Prod_Serv SET…
sqlasked 5 years, 5 months ago Marcondes Reis 1 -
0
votes0
answers202
viewsHow to pass the variable on postgres using dbblink
Oops, good morning! I have the following query in postgres: declare $1 VARCHAR; BEGIN IF (SELECT COUNT(*) FROM tempo2 WHERE looping IS NULL) > 0 THEN $1 = (SELECT banco FROM tempo2 WHERE looping…
-
0
votes1
answer116
viewsphp foreach Multilevel menu
Hello friends all right ? I am with the following problem I have this database need to create a menu with Multilevel for the blog site, created one with the following code: <ul class="nav…
-
0
votes1
answer47
viewsbecause it shows this message in inspecting elements when I’m making the connection with the database and Asp
Microsoft OLE DB Provider for SQL Server error '80040e14' The name "[email protected]" is not permitted in this context. Valid Expressions are constants, Constant Expressions, and (in some contexts)…
-
0
votes1
answer85
viewscreation of a composite KP
I have two tables: tbl1 and tbl2. In the tbl1 I own a pk and in the tbl2 I own a FK which refers to PK of tbl1 and a pk. With this, I can make a pk composed for the tbl2 with these 2 fields ? EX:…
-
0
votes0
answers52
viewsHow to add the value of an id_UsuarioLog to a FK?
Hello... Well, supposing a user wants to comment on any post. The system should: Check if the user is logged in, otherwise he will receive a script "you must be logged in to comment". If the user is…
-
0
votes1
answer46
viewsTrouble joining tables with UNION
I am trying to bring the top 3 of my table and also the last 2 placed, SITE TO EXECUTE SCRIPT This is my current progress that doesn’t work SELECT ('Podium: ' ||TEAM) as name FROM LEAGUE ORDER BY…
-
0
votes0
answers23
viewsSQL - Difference between Mandatorykey and Mandatoryfkey
I’m in my first internship since I started IT and the area I’m interning in is Databases. I have studied some old scripts and saw that some variables were declared, for example: @country AS…
-
0
votes0
answers50
viewsBring the value of one column according to the condition of another
I am developing a simple script but I came across a situation that I am not able to solve it, I tried through CASE but without success. With a simple select I need to bring the columns Name, RG,…
-
0
votes1
answer811
viewsERROR 1215: Cannot add Foreign key Constraint (Mysql Workbench)
Hello I am trying to add a foreign key to a table but I get error 1215. I have already checked the attributes and all are compatible, the portfolio field is a primary key, I do not understand why it…
-
0
votes1
answer395
viewsConsider NULL as 0
In the following query I need the transaction value to be considered as 0 when it is NULL in the highlighted aggregation, how can it be done? I tried using isnull but it didn’t work SELECT…
-
0
votes3
answers681
viewsSQL-Select To calculate seller commission
I need to calculate a seller’s salary + 10% on each service he performs. I thought about making a select with a COUNT and then doing the calculation. But I’m not getting it. Here’s what I tried,…
sqlasked 5 years, 5 months ago Isa Borges 31 -
0
votes2
answers4663
viewsSelect last record of a table - sql
Good afternoon! I have a table with several records of several patients. I need to bring only the last record of each patient. For example in the table: REG PACIENTE TITULO DATA 1000 joão evolução…
-
0
votes1
answer190
viewsInsert for each record found - PL/SQL
good morning. I’m new to Pl/sql and I need to insert 110 records (Number 1 to 110) in a table ( table: stick) for each ID of the court table. Insert would be more or less: INSERT INTO…
-
0
votes4
answers72
viewsShow login user column sum. error
Good morning, everyone, help me? i do not understand much of php, but for me to finish my site missing only one thing: I need to post the sum of the values that are in the table but based on the…
-
0
votes1
answer1814
viewsTake all data from an HTML table and insert it into the database
I have an editable table in html, when clicking save, I need to take all the data of the table, send by ajax to the page that makes the INSERT of all the lines in the database. My doubt would be:…
-
0
votes0
answers98
viewsHow to search user login to search data in database?
Good evening, everyone, I am using this command line, it worked perfectly with me typing the login of the person, but when I try to put to go based on the 'user_login' (logged in user) it does not…
-
0
votes1
answer55
views -
0
votes1
answer38
viewsSELECT SQL - Join with created fields
I need to make a Join with the product of two SELECTbut I’m not getting to an understanding. I need the columns created (notificacao) in tabs the tables are compared and return me the data of the…
-
0
votes2
answers92
views -
0
votes1
answer47
viewsJoin two Mariadb SQL queries
Two tables tab_Ord_Ser and tab_Pos_Alb. In two different queries I can know the values but I’m not able to unite them in just one. Are they: SELECT Prazo_Producao as Data, COUNT( `Numero OS` ) AS…