I have searched all over the internet and I have had no luck in finding the answer to my question. I hope this has not specifically been posted yet.
I am trying to change the data from row to columns.
Here is my current query:
SELECT * FROM wp_rg_lead_detail ORDER BY id
Gives me the result below:
id lead_id form_id field_number value
1 1 1 1 A
2 1 1 2 B
3 1 1 3 C
4 2 1 1 A
5 2 1 2 B
6 2 1 3 C
The lead_id is the spesific entry and the field number is the order of the value.
The result I am looking for is:
|Column 1|Column 2|Column 3|
----------------------
| 1 | 2 | 3 |
| A | B | C |
If my tables is confusing, please see attached images here:
Current result
Wished result
Any help or pointers would be highly appricated.
u can choose what u want to show in sql command.
SELECT `value`,`field_number` FROM wp_rg_lead_detail ORDER BY id
but if u want to change schema of table it's not work.
u can retrieve data with select query thene insert what u want in new table.
Related
I can't really explain it in words because it is hard for me. I'll just show you what I need to accomplish.
So lets say I have 2 tables, admin and records.
admin table with sample data:
a_id | a_name
1 | haime
2 | joseph
Record table with sample data:
r_id | r_amount | r_a_create_by | r_a_update_by
1 | 99 | 1 | 2
So I have a transaction record that is created by admin with ID of 1 and updated by ID of 2. Now how can I make a select query of that? If I want the output of something like :
1 | 99 | haime | joseph
You need to join the admin 2 times. This should work:
select r.r_id, r.r_amount, a.a_name, b.a_name
from records r
join admin a on r.r_a_create_by = a.a_id
join admin b on r.r_a_update_by = b.a_id
I am trying to show the results of items from one table where the count from another table equals a number from the first. I have been stuck on how to go about doing this for a couple weeks now so iv finally decided to ask for help. Im having a hard time explaining exactly what it is i need but i will try my best.
I am using PDO to interact with my database which is mysql.
For instance i have two tables:
table 1
-----------------
key | name | total
1 | item 1 | 3
2 | item 2 | 4
3 | item 3 | 2
table 2
-----------------
key | table1 key
1 | 1
2 | 2
3 | 3
4 | 1
5 | 1
6 | 3
7 | 2
8 | 2
So in this case there would be 3/3 items for item 1, 3/4 items for item 2, and 2/2 items for item 3. So it would show item 1 and item 3 as a result because the count for those two equal the total from table one.
I hope I explained this well enough.
If you want a sql query to do that, try this:
select t1.*
from table1 t1
inner join (
select table1_key, count(1) as cnt from table2 group by table1_key
) t2 on t1.key = t2.table1_key and t1.total = t2.cnt
SQLFiddel Demo
I have the following table structure:
structure_id | substructure_id | hash_id
1 1 1
1 1 2
1 2 1
2 1 3
2 1 1
2 2 2
And I want to fetch an intersection for each structure id how many hashes intersect with other structure ids, with the same hashes in different substructures being distinct. What I mean is the following output:
structure_id | structure_intersected_id | count
1 1 2
1 2 2
2 2 3
2 1 2
Is this possible to achieve with MySQL? So far I have the following query:
select t1.structure_id, t2.structure_id, count(*)
from table t1
inner join table t2 on t1.hash_id = t2.hash_id
group by t1.structure_id, t2.structure_id;
But it doesn't remove duplicated substructure hashes, and is not correct for 1-1, 2-2, 3-3, etc. structure_id pairs.
How can I solve this?
I'm building a little quiz game in PHP/MySQL. After asking questions I want a screen to display how many of the last round were answered correctly.
Im storing whether they were answered correctly or not in a table that looks like this:
rowID | questionid | playerid | answercorrect |
1 | 1 | 1 | 1 |
2 | 2 | 1 | 1 |
3 | 3 | 1 | 1 |
4 | 4 | 1 | 1 |
5 | 5 | 1 | 0 |
6 | 6 | 1 | 1 |
7 | 7 | 1 | 1 |
I want to see how many of the last x (usually 5) questions were answered correctly.
I thought this would be simple. I'm trying this:
SELECT sum( answercorrect ) FROM `answersgiven` ORDER BY id DESC LIMIT 5
I thought this would sum the answercorrect column for the last 5 rows, giving me an answer of 4, but it's giving me 7, which is the result for ALL of the rows.
I feel like I'm missing something obvious. It seems like a simple thing to want to do.
Any ideas?
Try this:
SELECT sum(answercorrect)
FROM (SELECT answercorrect FROM `answersgiven` ORDER BY rowID DESC LIMIT 5) t1
Example Fiddle
In your query, the LIMIT clause affects the overall result: So first all are summed up (which results in one row - the sum over all rows) and then the first 5, if available, are taken (which again is just the one row).
The easiest way of achieving your target is to first select just the first 5 rows (in the subselect) and then sum up afterwards.
try this query
SELECT sum(col)
FROM (SELECT col FROM `table` ORDER BY id DESC LIMIT 5) t1
You're almost there. Just sum up those top 5 answers now:
SELECT SUM('top5')
FROM
(SELECT answercorrect AS 'top5'
FROM `answersgiven`
ORDER BY id DESC LIMIT 5) 'x'
I am fetching all stations which belong to a station group from my database. SELECT * FROM stations WHERE station_group_id = 1.
Now, from all the fetched results, I want certain ones to appear first (e.g. the stations which have line_id = 2 to appear first). For example, if this is my stations table:
id | station_group_id | line_id
-------------------------------
1 | 1 | 1
2 | 1 | 2
3 | 1 | 3
I would like the output to be:
id | station_group_id | line_id
-------------------------------
1 | 1 | 2
2 | 1 | 1
3 | 1 | 3
So that line_id = 2 is the first record in the output.
I thought about using ORDER BY, but it isn't quite an order issue, it is more a "preference" one.
So, is it possible to place some records on top of the output, based on a condition, preferably in one query? Thanks!
Try Below:
SELECT * FROM stations
WHERE station_group_id = 1
ORDER BY if(line_id in('2','X','Y','Z'),0,1)
SELECT * FROM stations WHERE station_group_id = 1 and line_id = 2
union
SELECT * FROM stations WHERE station_group_id = 1 and
line_id != 2 order by line_id asc
As you are saying, it is actually a preference, so you should either model it as an extra field on the table (e.g. ordinal, or order, or preferredOrder), or you keep sorting by line_id, and do the "special sort" in code. (find element with id=2, move to top)