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
answers213
views#1215 - You cannot add a foreign key constraint
Good night, I can’t figure out why error #1215. I’m a few hours away and still can’t figure it out. Can anyone help me? Thanks! CREATE TABLE IF NOT EXISTS `imovelweb`.`cadastro` ( `id_cadastro` INT…
-
0
votes0
answers442
viewsMust declare the scalar variable "@doc"
I searched a lot about this error but nothing seemed similar to what is happening to me, I created a cursor and after fetch next returns me the title error, someone could help? declare @doc int…
-
0
votes1
answer42
viewsHelp in this query in SQL Server
That consultation: SELECT C.CompanyName, MAX(SOH.TotalDue) AS Total, A.CountryRegion FROM SalesLT.SalesOrderHeader SOH INNER JOIN SalesLT.Customer C ON C.CustomerID = SOH.CustomerID INNER JOIN…
-
0
votes1
answer47
viewsConsult data in three tables where only two are related
Good afternoon, I will try to explain a situation where I am, I have 3 tables(A,B,C) where A and B are directly related, B and C are also related. I need to get the value of a field in table C where…
-
0
votes1
answer63
viewsJOIN with two fields in the ON clause with Codeigniter (mysql+php) (MVC)
I am in need of help with Codeigniter. "Fell into my lap" legacy application. Below is an attempt of mine to illustrate the problem, IE, table names and fields are "scenographic". How to make a Join…
-
0
votes2
answers104
viewsConvert a Query with a Subquery to a Query with a Join
I’m trying to optimize a Query that the system I’m working on makes it have a SubQuery inside, only this SubQuery is referencing another table that has relation to the main table and by what I’ve…
-
0
votes1
answer36
viewsIncorrect grouping of lines
Hello someone can help me, I’m trying to bring a company report and activity made in 12 months, a company and an activity was made during 12 months and I want to know each activity if it was made in…
-
0
votes1
answer91
viewsSubtraction of values in the same table and column
I have a table in phpMyAdmin called flow, in this table is the flow of a box (The description would tell us what each record is for) I need to have a way to achieve the net profit of each month in a…
-
0
votes1
answer49
viewsMaximum value per category
I am doing a job in which I would like to show the maximum balance of each account type with subselect within the SELECT. Something more or less like this: CodConta ---------------------tipo…
-
0
votes1
answer54
viewsSelect 2 wheres in a query with more than 1 table - PHP, SQL
I am developing a legal system for registering processes and that can be shared between the users involved. At the moment, I need to filter processes between dates. I already managed to do this…
-
0
votes2
answers406
viewsSelect 2 latest records from Sql Server table
I have a table with the following fields. IMEI | TimeStamp | Value 123 | 1570011000| 1 123 | 1570012800| 0 123 | 1570015200| 1 123 | 1570017000| 0 I would like to take the last 2 records and make…
-
0
votes0
answers38
viewsReturn of a wrong create Function
I have the following table in the schema utfpr: create table Matricula( dis_sigla varchar(50), tur_numero varchar(15), alu_ra varchar(100) , mat_ano date, mat_nota float not null, constraint…
-
0
votes1
answer113
viewsstore select result in a table
Good afternoon I have the following query SELECT +cast((DATEDIFF(SECOND,r.start_time,GETDATE())/60)%60 as varchar)+'m ' duracao ,isnull(s.login_name, s.original_login_name) AS LOGIN ,r.session_id…
-
0
votes0
answers161
viewsIs it possible to execute 2(delete,Insert) commands in a Stored Procedure - MYSQL?
Falling into the condition of if I need to execute these two commands: if(ultima_refeicao!=refeicao) then SELECT refeicao_alimentos.calorias into calorias from refeicao_alimentos where…
-
0
votes1
answer52
viewsI want to display a value according to the current date
I want to select the amount of Num_CP according to the current date, so I am using the function GETDATE(), but my SQL points out the following error: Only one Expression can be specified in the…
-
0
votes1
answer900
viewsHow do I use order by in two columns in the same table? Postgresql
Hello, I have a problem and would like your help. I am beginner with database and need to make a report. The report is of all budgets organized by city and by date. Wanted more or less like this:…
-
0
votes1
answer40
viewsDoubt - Query Sqlite
I’m having questions on how to write a query. Follow the problem information: I have 3 tables, the first being an athlete registration table (id, name, age, country...), a second table with…
-
0
votes1
answer71
viewsHow to group array with the same ids in new array?
I’m using a php function to get custom Fields data from whmcs sql, but the operation returns the data in an array, separating each id by products. That is, it does not return the product with all…
-
0
votes1
answer85
viewsHow to make a select if repeat integer 1 time for each month in a dynamic date range?
Assuming I have sales that happened in the month of May. I could make the following select: SELECT * FROM Vendas WHERE MONTH(Data_Venda) = 5 Assuming the month of June happened and now I have sales…
-
0
votes1
answer548
viewsHow do ORDER BY DESC work in Mysql?
I am trying to make a query on my table using OREDER BY DESC, even using the correct syntax I get the error: "You have an error in your SQL syntax; check the manual that Corresponds to your Mysql…
-
0
votes1
answer196
viewsHow to place an AUTOINCREMENT ID?
I’m making a login table, where you log in, add your service data and add a company to which you work. But I’m having trouble adding the user id. I want that so q the person make his registration,…
-
0
votes0
answers1410
viewsError inserting in table: "Missing FROM-clause entry for table..."
I have three tables: CREATE TABLE Bebidas( id SERIAL primary key, nome varchar(40) NOT NULL, preco_bebidas decimal(5,2) check (preco_bebidas > 0.00), qntd int); CREATE TABLE Shots( id SERIAL…
-
0
votes1
answer190
viewsPostgres/Mysql Changing the data of a repeating field
Good morning I have the "areas" table, In it I have the column "f_name" I want to transform the "f_name" column to Unique id. However, there are repeated data currently. I need an Sql command where…
-
0
votes1
answer125
viewsIs there any way to select a comparison of duplicate records?
I have a table with 57 fields, due to an oversight in the past did not create the REQUEST field as Unique Key Due to the use by more than one user on a screen of the system he ended up allowing the…
-
0
votes0
answers90
viewsMysql - Select tuple values using dynamic column names
It is a filter where the user selects the days of the week and sends to the bank to filter but I am not able to perform the filtering. The table has as column the days of the week with several…
-
0
votes2
answers160
viewsHow to delete duplicate lines in Postgres database?
In my bank I have a table that has a list of (carrier and waste specifications) where the carriers can have several waste, I checked that the data are duplicated and are represented in this way:…
-
0
votes0
answers127
viewsCharacter 'º' is accepted by utf8 within the Insert, but not when I use COPY
I’m trying to migrate data from a Firebird database to a Postgresql version 12.0. To generate a. csv file containing the table records in Firebird, I use the software Fbexport, ./fbexport -Sc -D…
-
0
votes1
answer32
viewsGroup By com sql server
I am trying to group the data of my select , according to the field C9_CARGA , however when I run this select it returns me without being grouped , I believe I am doing something wrong . How are you…
-
0
votes1
answer91
viewsSqlserver: How to detect consecutive days in a table
I need to insert into a time table only the rows of another table in which the Membershipdate column has 1 day difference dates. For example, in the table image below, only lines 1, 2 and 3 would go…
-
0
votes1
answer302
viewsWhat is the SQL Server compatible command for Mysql SHOW CREATE TABLE?
What would be the command in SQL Server to view the script for the creation of a given table? With a result more or less like this: +------------+---------------------------------------------------+…
-
0
votes2
answers41
viewsChange the value of a field when a condition is true
Good evening, everyone, Can you help me? I need to create a view with the data below, however I need to create a condition where when the value of MC.SITUACAO is equal to 'A', the fields…
-
0
votes1
answer102
viewsIs there a performance gain in concatenating strings directly into SQL?
Actually, I have a code kind of like this: const users = await query(` SELECT "id", "username", "email" FROM "users"; `) .then((users) => users.map((user) => { user.id =…
-
0
votes1
answer136
viewsSQL: Copy records from a column
Using: Mysql I have this table called Licencas I need to get the column empresa_id and the coluna_id of this table And pass this data from these two columns to another table Calling for…
-
0
votes1
answer65
viewsI need to make a query in the database. mysql
I have the following database. CREATE TABLE IF NOT EXISTS futebol ( id INT PRIMARY KEY AUTO_INCREMENT, nome VARCHAR(30) NOT NULL, estado VARCHAR(2) NOT NULL, brasileirao INT NOT NULL, copadobrasil…
-
0
votes1
answer96
viewsMultiple COUNT() in a single query
Good morning, I have the following table: +------------+--------------+------+-----+----------------+ | Field | Type | Null | Key | Extra |…
-
0
votes1
answer55
viewsSearch records filtering by Count from a subconsulta
I have the following problem: I have three tables and need to show the employee record only when it has 1 and only 1 dependent. And it should also have 0 and at most 1 prize record, according to the…
-
0
votes1
answer106
viewsSqlite3 How to delete last records using ORDER BY
I’ve researched a lot in the inter but found no solution... For example, I have an aptitude test. Hence I create a table: "Bdteste.db", with a bank: "candidates", with the fields: _id, name and…
-
0
votes0
answers79
viewsProblem with PDO::FETCH_ASSOC
Hello, everybody. I’m making a tag system for my blog. To get the tag id, I’m making a query using the IN operator. I want you to return only the associative array.That’s why I use PDO::FETCH_ASSOC.…
-
0
votes1
answer73
viewsNotice: Object of class mysqli_result could not be converted to int in database.php
I’m having very specific problems while working on my crud. I created in the database a field called id, type INT and A_I and try to call it through this part of the code, but it keeps giving this…
-
0
votes1
answer124
viewsQuery to bring 1 parent record and in it can contain multiple children records (performance)
Supposing for the client I can add more joins as not only sales, and the result of this consultation bring much more records. This is a performative way of working or would be better to query each…
-
0
votes1
answer51
viewsTake multiple rows from a table in the database with the same name and display the name only once in a list
My form has the option to add more rows, so I had to use array and send its information to the database. In this case the database has created 3 rows within the table with various information, but…
-
0
votes1
answer269
viewsQuery Excel with cell criteria
I have a spreadsheet based on an SQL code powered by our database. But every month I have to enter the code and change the dates to get this information right. I want Power Query to use the values…
-
0
votes0
answers52
viewsSQL ERROR 1442. What can it be?
The error appears when I try to insert something in bank 3, because Trigger will be activated and will insert in bank2, but I am not performing 2 shares in the same table as the error tells...…
-
0
votes1
answer87
viewsMethod to insert into the database is not pulling value [JAVA]
Good night! I’m trying to do a college job (JAVA), and I have to insert the client data into the database (Mysql), I have a Client class, a form, and the database class. Form calling method for…
-
0
votes0
answers546
viewsSQL tables for postal codes
Good, I am creating a BD for a project I am doing and in this project are included Addresses (Customers, Suppliers, etc.), addresses that will contain Postal Code. My question is this:: I have 3…
-
0
votes1
answer118
viewsHow to take query repetition
I created a chat, and I have a query to display the other accounts of the site to start a conversation. I put a select all, and it displays all normally, but now I need to put an order by to sort…
-
0
votes1
answer98
viewsSQL Error no Postgres
My select: SELECT * ( SELECT v.placa FROM gr_veiculo v WHERE v.id = ( SELECT DISTINCT r.cavalo FROM lg_relacao_veiculo r WHERE ( r.cavalo = (SELECT mv.gr_veiculo_id FROM…
-
0
votes0
answers152
viewsValidate record that has comma in the Oracle PL SQL field
I’m creating a procedure in PL SQL and I have an "unregulated" field that may have more than one comma-separated record. That one procedurecarries out the cancellation of sending extracts of a…
-
0
votes1
answer390
viewsHow to place ORDER BY in INNER JOIN/GROUP BY
In a chat, I need to give a SELECT in the contacts of the logged in user, but ordering them by the date of the messages The code is like this: SELECT contatos.*, contas.Key_User, contas.Login_User,…
-
0
votes1
answer67
viewsmysql - Query 3 tables at the same time
I have 3 tables, and need to make a SELECT inside a LOOP on 3 at the same time, would be as follows: note that the result, I take the username where it would be in the column NAME, I DO a num_rows…