I have a very strange scenario and would be highly grateful if you can help me out. I have one flat table of five fields, and I need to select everything from say the last three columns.
SAMPLE:
ID CODE BRAND T1 T2
== ==== ===== == ==
1 KUU RR4 1 2
2 KUU R56 2 1
=====================
I need only the values of T1 and T2. The reason I need to get the last two columns is that the table is generated automatically from an Excel file, so the first three columns are static (ID, CODE, BRAND), and only last two columns are dynamic (T1, T2) and can be different next time, like (P1, P2), so I cannot do something like SELECT T1, T2 FROM sampleTable. I am totally stumped any help or advice would be great.
You didn't mention php, but there it is in the tags so:
In your php script, you could select the whole shebang and still access the fields in a numeric order. For example:
$q=mysqli_query("SELECT * FROM SAMPLE");
while($row=mysqli_fetch_array($q,MYSQLI_NUM))
{
printf("col#4: %s -- col#5: %s",$row[3],$row[4]);
}
SELECT 4,5 FROM SampleTable;
4,5 specifies the column number in your table.
Related
I want to create a statistics page where i can count the number of reasons given in a table ...The reasons are created dynamically and stored in another table.
Table1 contains all the reasons which populate a drop down box...
Table2 contains the entries given by the user one of the columns is the reason value.
I would like to count the amount of times each reason appears .
Thankyou for your time and help
Looks like simply group statement:
select
reason.name reason,
count(*) count
from entry, reason
where
entry.reason = reason.id and
entry.time > now() - interval 7 day -- you probably need some filter
group by reason.id
I know auto_increment is the way to go but I can not use auto_increment feature since the column in my table might repeat, its not unique. When I insert a new row to a table I need a way to find the next available spot to insert it.
For example table structure:
Primary Key = (ID, UserID)
ID UserID
3 6
3 1
1 3
Now when i do insert query i want to isert it at ID = 2 and not 4. With auto_increment it gives me 4
Is there a solution without using the loop in PHP? So far what i have is I fetch all rows into array and then find the next available digit in ID. Is it possible to do this without fetching all rows in PHP and just doing it on MySQL query ?
SELECT t1.id+1 AS MISSING_ID
FROM the_table AS t1
LEFT JOIN the_table AS t2 ON t1.id+1 = t2.id
WHERE t2.id IS NULL
ORDER BY t1.id LIMIT 1;
I made a fiddle: http://sqlfiddle.com/#!2/4d14d/2
No, it is not possible without processing the data. The preferred method to correct this issue is to adjust your table structure to support a unique, auto-incrementable field. Failing that, you will have to process the data (either in PHP or via an SQL statement) to find an open slot.
This should do the trick:
SELECT
min_table.ID+1 AS start,
MIN(max_table.ID) - 1 AS end
FROM
your_table AS min_table,
your_table AS max_table
WHERE
min_table.ID < max_table.ID
GROUP BY
min_table.ID
HAVING
start < MIN(max_table.ID)
The left hand column will return the first available spot in the sequence gap, and the second is the highest number in that particular gap.
Source: http://www.codediesel.com/mysql/sequence-gaps-in-mysql/
My workaround for not loaded project:
Suppose, you have questionset with question_id 's which belong to certain topic_id.
Suppose, user navigates and clicks "<Prev" "Next>" buttons to navigate questions.
You have only current id. Catching the direction of navigation, topic_id, question_id you can do a loop
do {
// query base, doing question_id++ or question_id-- depending on needed direction until you find next id within topic_id
} while( id!=null ) `
using incrementation or decrementation depending on direction of your move
I've such a interested question for me , please if anyone know answer me .
So I have two table , in first i have column named "num" which includes (like say phone numbers) and in second table i have column "sec_num" which also includes some of the exactly same numbers like in first table from column "num" and second column named "persons" (from second table also)
I am retrieving data from first table and i want that if first table phone numbers (from num column) matched to second table numbers (from column "sec_num") then retrieved value from "persons" column. Is it possible to do ? if yes , please help me , Thanks ...
PS. if it is necessary to know i am using php, but I think this job must do sql
If I understand the question right a regular left join should do the trick;
select num, persons from first left join second on num=sec_num;
If we call the tables tableA (first table) and tableB (second table) thern a query like:
SELECT * FROM tableA INNER JOIN tableB on tableA.num = tableB.sec_num
should do the trick.
I took over managing an internal website for the company that I'm working for and I need to get data out of a mysql database. The problem that I'm encountering is that the data is in 6 different tables, all with the same fields but the rows are all unique (the row starts in one table and then gets completely moved to a different table after it is processed by an employee).
Is there an easy way to query against all 6 at once? It would also be useful to be able to retrieve the title of the table it came from.
I'm using PHP to run the query and display it. Would it be better to create another table that defines where all the rows are, have a unique id and then another field for which table it's in?
To complete this query, use union all:
select
'Table1' as TableName,
*
from
Table1
union all
select
'Table2',
*
from
Table2
union all
select
'Table3',
*
from
Table3
...and so on
For better database design, you would want to either have one table with all the rows in it, and a designated Status table where you can link a StatusID column to that says what status that given row is in. A table for each stage in a process is a poor design and will only lead to massive headaches down the road.
If you can't reorganize the tables so that you have just one with all rows and a marker for where in the process they are, I would go for a UNION-approach. Ie:
SELECT 'Data from Table 1', t1.field1, ...
FROM Table1 t1
UNION
SELECT 'Data from Table 2', t2.field1, ...
FROM Table2 t2
UNION
(Table3, 4, 5 and 6 in the same manner)
....
That way you can see where the data is originating from and you get all 6 at once. Just remember that you have to have the exact same field list in all parts of the UNION.
You could create a code generator that generates SQL statements to query the 6 tables. The generator would create a UNION of 6 selects and add a "table" column to each select with a constant value equal to the name of the table queried. That would make writing the statements easy, though I wouldn't say that writing the generator would be.
I have working in database Postgresql for the first time. I need your help to finding out a solution. One table contains 15 rows With a regn_srno as P.K., another table has the same regn_srno as F.K. I want to count the total number of rows which has the same regn_srno. But My problem is the second table has contain 2 or 3 fields with the same regn_srno. So when i use count in the query it shows 12 (including the same regn_srno), but the original number is 10. Due to the same regn_srno repeat in the second table i got the answer as 12.
When we group by regn_srno we get the result as 1,1,1,1,2,1,2,1,1,1. So i need the query to get the count as 10.Please help me. Please send me the answer through my mail id.
For what I could figure out without tables schema, I think you want
SELECT count(DISTINCT regn_smo) FROM t1 JOIN t2 USING (regn_smo);
You could simply do:
SELECT count(DISTINCT regn_smo) FROM t2